Question

I have a function that hooks into save_post that changes the value of a custom taxonomy term based on today's date. That's working fine. When I update a post, the taxonomy term is updated correctly.

But I want this update to happen automatically, once a day, without having to manually update the posts myself. I read up on wp_cron and created the below code which is basically an attempt to, once a day, query for a certain set of posts and then simply run them through wp_update_post() which should trigger the function mentioned above that changes the term value of the taxonomy.

But it doesn't seem to be working. Also, I don't really know how to test wp_cron without just waiting for a day and seeing what happens. I have a console installed so I can see upcoming scheduled events, and the below event is definetely scheduled, it just doesn't seem to be having any effect.

Can anyone offer some advice on where I might have gone wrong?

    //Schedule active events to update_post every day
    if( !wp_next_scheduled( 'event_status_refresh' ) ) {  
       wp_schedule_event( time(), 'daily', 'event_status_refresh' );  
    }  
//When the event fires, we call the function to update the posts
    add_action( 'event_status_refresh', 'update_event_status' ); 

//The function to update the posts
function update_event_status() {
    global $post;

    $active = array( 
    'post_type'=>'event',
    'posts_per_page'=>'-1',
    'tax_query'=>array(
        array(
            'taxonomy'=>'event_status',
            'field'=>'slug',
            'terms'=>array(
                'playing_now',
                'opening_soon'
            )
        )
    ),
);
$query   = new WP_Query($active);
if ($query->have_posts()) {
    while ($query->have_posts()) {
        $query->the_post();

            $update = array(
            'ID' => get_the_ID(),
            );

            // Update the post into the database
            wp_update_post( $update );
    }
}
wp_reset_postdata();

}

No correct solution

Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange
scroll top