• 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. 

    • This topic was modified 3 years, 4 months ago by Vimal Roy.

    The page I need help with: [log in to see the link]

Viewing 1 replies (of 1 total)
  • 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.ww.wp.xz.cn/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.