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.
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.
The page I need help with: [log in to see the link]
The topic ‘Republishing posts using wp_update_post not working !!!’ is closed to new replies.