Title: Republishing posts using wp_update_post not working !!!
Last modified: February 1, 2023

---

# Republishing posts using wp_update_post not working !!!

 *  [Vimal Roy](https://wordpress.org/support/users/vimalroy08/)
 * (@vimalroy08)
 * [3 years, 4 months ago](https://wordpress.org/support/topic/republishing-posts-using-wp_update_post-not-working/)
 * We have a category called “price prediction,” and in that category, there are
   150+ posts. We’d like a function that will republish all of these posts with 
   the current date.
 * We created a function, as mentioned below.
 *     ```wp-block-code
       function update_post_dates_in_category_batch($category_slug, $batch_size) {
           $offset = 0;
           while (true) {
               $query = new WP_Query(array(
                   'category_name' => $category_slug,
                   'post_status' => 'publish',
                   'posts_per_page' => $batch_size,
                   'offset' => $offset,
                   'no_found_rows' => true,
               ));
               if (!$query->have_posts()) {
                   break;
               }
               while ($query->have_posts()) {
                   $query->the_post();
                   $post_id = get_the_ID();
                   $new_date = current_time('mysql');
                   $new_date_gmt = current_time('mysql', 1);
                   $args = array(
                       'ID' => $post_id,
                       'post_date' => $new_date,
                       'post_date_gmt' => $new_date_gmt,
                       'post_modified' => $new_date,
                       'post_modified_gmt' => $new_date_gmt,
                   );
                   wp_update_post($args);
               }
               wp_reset_postdata();
               $offset += $batch_size;
           }
       }
   
       if (!wp_next_scheduled('update_post_dates_in_category_batch_event')) {
           wp_schedule_event(time(), 'daily', 'update_post_dates_in_category_batch_event');
       }
       add_action('update_post_dates_in_category_batch_event', 'update_post_dates_in_category_batch_callback');
   
       function update_post_dates_in_category_batch_callback() {
           update_post_dates_in_category_batch('price-prediction', 10);
       }
       ```
   
 * This function will retrieve a batch of 10 posts from the “Price Prediction” category
   and republish them on a daily basis. But we have a problem: the fetched posts
   contain approximately 20–25 items, and they are getting republished. The function
   is not working with the values given.
 * Is there any issue with the code? I tested the function with the full number 
   of posts, and it still works as it should, like modifying only 20-25 posts before
   it stops working. 
    -  This topic was modified 3 years, 4 months ago by [Vimal Roy](https://wordpress.org/support/users/vimalroy08/).
 * The page I need help with: _[[log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fwordpress.org%2Fsupport%2Ftopic%2Frepublishing-posts-using-wp_update_post-not-working%2F%3Foutput_format%3Dmd&locale=en_US)
   to see the link]_

Viewing 1 replies (of 1 total)

 *  [Alan Fuller](https://wordpress.org/support/users/alanfuller/)
 * (@alanfuller)
 * [3 years, 4 months ago](https://wordpress.org/support/topic/republishing-posts-using-wp_update_post-not-working/#post-16431261)
 * Even though you have a batched query the process still runs in one PHP session
   and your server is probably limited to 30 seconds PHP for web calls.
 * There are several things you can do.
 * If you have WP CLI and command line access you could write it as a WP CLI see
   [https://make.wordpress.org/cli/handbook/guides/commands-cookbook/](https://make.wordpress.org/cli/handbook/guides/commands-cookbook/)
 * As a command line execution of PHP does not have a time out.
 * The alternative is for you to schedule a daily job that counts the number of 
   posts and then that schedules immediately execution of multiple single events
   e.g. my_batch_1, my_batch_2 etc which each processes 10 passing the appropriate
   offset, or rather than offset you could query the initial query could be posts
   < current time

Viewing 1 replies (of 1 total)

The topic ‘Republishing posts using wp_update_post not working !!!’ is closed to
new replies.

## Tags

 * [cron](https://wordpress.org/support/topic-tag/cron/)
 * [date](https://wordpress.org/support/topic-tag/date/)
 * [posts](https://wordpress.org/support/topic-tag/posts/)
 * [query](https://wordpress.org/support/topic-tag/query/)
 * [update](https://wordpress.org/support/topic-tag/update/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 1 reply
 * 2 participants
 * Last reply from: [Alan Fuller](https://wordpress.org/support/users/alanfuller/)
 * Last activity: [3 years, 4 months ago](https://wordpress.org/support/topic/republishing-posts-using-wp_update_post-not-working/#post-16431261)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
