Scheduling posts to update once per day with wp_cron

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.

Read More

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();

}

Related posts

1 comment

  1. This is a typical example of an X-Y problem. Your cron is fine. Your logic not so.

    You have a function that hooks into save_posts and you think that passing the array $update = array( 'ID' => get_the_ID() ) will trigger the action and so your post will update the taxonomy. That’s incorrect, unfortunately.

    Passing this type of array (only the ID field) to wp_update_post will only identify the post, it has no data to save elsewhere in the array. So it won’t update anything and won’t trigger any save_post actions.

    So your cron is running every day, but doing nothing.

    Solution:

    The function that hooks into save_post probably accept a post_id as param and update post if needed, right. So, instead of running wp_update_post and letting it update the post, call your function by itself.

    while ($query->have_posts()) {
      $query->the_post();
      $toupdate = get_the_ID();
    
      your_function_that_hook_into_save_post( $toupdate );
    
    }
    

Comments are closed.