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ía "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 );
// Actualizar los términos de la taxonomía "tipo_evento" de los webinars realizados
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 );
}
}
And this is the code for the cron event:
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've tried to query the posts and change the terms of them.
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->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 );
}
}