cron事件传递的函数不执行任何操作。

huangapple go评论62阅读模式
英文:

The function passed to the cron event doesn't do anything

问题

我有这个函数 actualizarwebinarsProximosARealizados(),在这个函数中,如果日期条件为真,我想要更改帖子的术语,添加 'webinar-realizado' 并删除 'webinar-proximo',但是当执行定时事件时什么都不会发生。

这是函数代码:

function actualizarwebinarsProximosARealizados() {
    // 获取具有 'webinar' 分类的帖子
    $args = array(
        'post_type' => 'formacion',
        'tax_query' => array(
            'relation' => 'AND',
            array(
                'taxonomy' => 'tipo_evento',
                'field' => 'slug',
                'terms' => 'webinar',
            ),
            array(
                'taxonomy' => 'tipo_evento',
                'field' => 'slug',
                'terms' => 'webinar-proximo',
            ),
        ),
        'meta_query' => array(
            array(
                'key' => 'fecha',
                'value' => date('Ymd'),
                'compare' => '<',
                'type' => 'DATE',
            ),
        ),
        'posts_per_page' => 50,
        'fields' => 'ids',
    );

    $webinars_proximos_clasificados = new WP_Query( $args );

    // 更新已完成的网络研讨会的 'tipo_evento' 分类术语
    if ( $webinars_proximos_clasificados->have_posts() ) {
        $ids = $webinars_proximos_clasificados->posts;
        $taxonomy = 'tipo_evento';
        $terms_to_remove = 'webinar-proximo';

        wp_remove_object_terms( $ids, $terms_to_remove, $taxonomy );

        wp_set_object_terms( $ids, 'webinar-realizado', $taxonomy, true );
    }
}

这是定时事件的代码:

add_action('init', 'agregar_taxonomia_a_webinars_condicional_cron');
function agregar_taxonomia_a_webinars_condicional_cron() {
    if (!wp_next_scheduled('agregar_taxonomia_a_webinars_condicional')) {
        wp_schedule_event(strtotime('today 3:00'), 'daily', 'agregar_taxonomia_a_webinars_condicional');
    }
    add_action('agregar_taxonomia_a_webinars_condicional', 'actualizarwebinarsProximosARealizados');
}
英文:

I have this function actualizarwebinarsProximosARealizados() where I want to change the post term adding 'webinar-realizado' and removing 'webinar-proximo 'if the date condition is true but when the cron event is executed nothing happens.

This is the function code:

function actualizarwebinarsProximosARealizados() {
// Obtener los posts que tienen la taxonom&#237;a &quot;webinar&quot;
$args = array(
&#39;post_type&#39; =&gt; &#39;formacion&#39;,
&#39;tax_query&#39; =&gt; array(
&#39;relation&#39; =&gt; &#39;AND&#39;,
array(
&#39;taxonomy&#39; =&gt; &#39;tipo_evento&#39;,
&#39;field&#39; =&gt; &#39;slug&#39;,
&#39;terms&#39; =&gt; &#39;webinar&#39;,
),
array(
&#39;taxonomy&#39; =&gt; &#39;tipo_evento&#39;,
&#39;field&#39; =&gt; &#39;slug&#39;,
&#39;terms&#39; =&gt; &#39;webinar-proximo&#39;,
),
),
&#39;meta_query&#39; =&gt; array(
array(
&#39;key&#39; =&gt; &#39;fecha&#39;,
&#39;value&#39; =&gt; date(&#39;Ymd&#39;),
&#39;compare&#39; =&gt; &#39;&lt;&#39;,
&#39;type&#39; =&gt; &#39;DATE&#39;
),
),
&#39;posts_per_page&#39; =&gt; 50,
&#39;fields&#39; =&gt; &#39;ids&#39;,
);
$webinars_proximos_clasificados = new WP_Query( $args );
// Actualizar los t&#233;rminos de la taxonom&#237;a &quot;tipo_evento&quot; de los webinars realizados
if ( $webinars_proximos_clasificados-&gt;have_posts() ) {
$ids = $webinars_proximos_clasificados-&gt;posts;
$taxonomy = &#39;tipo_evento&#39;;
$terms_to_remove = &#39;webinar-proximo&#39;;
wp_remove_object_terms( $ids, $terms_to_remove, $taxonomy );
wp_set_object_terms( $ids, &#39;webinar-realizado&#39;, $taxonomy, true );
}
}

And this is the code for the cron event:

add_action(&#39;init&#39;, &#39;agregar_taxonomia_a_webinars_condicional_cron&#39;);
function agregar_taxonomia_a_webinars_condicional_cron() {
if (!wp_next_scheduled(&#39;agregar_taxonomia_a_webinars_condicional&#39;)) {
wp_schedule_event(strtotime(&#39;today 3:00&#39;), &#39;daily&#39;, &#39;agregar_taxonomia_a_webinars_condicional&#39;);
}
add_action(&#39;agregar_taxonomia_a_webinars_condicional&#39;, &#39;actualizarwebinarsProximosARealizados&#39;);
}

I've tried to query the posts and change the terms of them.

答案1

得分: 0

wp_remove_object_terms和wp_set_object_terms都接受一个整数作为第一个参数,根据文档 - 你似乎传递了一个数组。– CBroe

这就是我的代码问题,我创建了一个循环来应用它到所有的文章,现在它可以工作了。

我更改的代码:

if ( $webinars_proximos_clasificados->have_posts() ) {
    $ids = $webinars_proximos_clasificados->posts;
    $taxonomy = 'tipo_evento';
    $terms_to_remove = 'webinar-proximo';

    foreach ( $ids as $post_id ) {
        wp_remove_object_terms( $post_id, $terms_to_remove, $taxonomy );
        wp_set_object_terms( $post_id, 'webinar-realizado', $taxonomy, true );
    }
}
英文:

> Both wp_remove_object_terms and wp_set_object_terms take an integer as first parameter, according to the documentation - you appear to be passing an array. – CBroe

That was the problem with my code I created a loop for applying it too all the posts and now it works.

The code I changed:

	if ( $webinars_proximos_clasificados-&gt;have_posts() ) {
$ids = $webinars_proximos_clasificados-&gt;posts;
$taxonomy = &#39;tipo_evento&#39;;
$terms_to_remove = &#39;webinar-proximo&#39;;
foreach ( $ids as $post_id ) {
wp_remove_object_terms( $post_id, $terms_to_remove, $taxonomy );
wp_set_object_terms( $post_id, &#39;webinar-realizado&#39;, $taxonomy, true );
}
}

huangapple
  • 本文由 发表于 2023年6月5日 16:29:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/76404689.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定