Coders. I'm really new in WP Coding, I have zero knowledge, here we are. I created a plugin (actually found it, but I did some modifications) which update all my wp posts.

Let's show you the code,

if ( ! class_exists( 'MyPlugin_BulkUpdatePosts' ) ) :

class MyPlugin_BulkUpdatePosts
    public function __construct()
        register_activation_hook( __FILE__, array( $this, 'do_post_bulk_update' ) ); //Run this code only on activation

    //Put your code in this function
    public function do_post_bulk_update()
        $posts_to_update = get_posts('numberposts=-1'); //numberposts for post query, "-1" for all posts.
        foreach ( $posts_to_update as $update_this_post ):

        //update query goes here


As you can see, it makes a query to the all posts, The main problem is I have 10k+ posts, But when I use this my server gets crashed, It gives a "503 Unavailable". But When I use 50-60 posts, it's works.

How can I make this work by less resources ?


One implementation detail of how WP works with database is that it always drags all query results into PHP values and memory space. In other words it is highly unlikely to throw any heavy query at WP and not have it collapse. Notably any plugins that deal with large queries (such as database backup ones) often write their database access layer from scratch instead of using WP API.

So implementing bulk operations has to be a little more elaborate. Querying should be split into smaller batches and they should be processed sequentially. You would need to write that logic yourself or find an existing solution. I think there are some around, but I hadn't used any generic ones.

The alternate approach, in some cases, is to hook update logic to access of individual posts. If giant one-time complete update of everything is not required, updates can be spread over time in such fashion with no need for throwaway update code and no concern about resource impact.

| improve this answer | |

10k+ updates must take time. Depending what you need you could operate on the Wordpress tables like wp_posts using raw SQL queries but I don't think it would speed up things substantially.

Because the problem is I think time - if that is a one time operation - you could solve the problem by changing the max_execution_time in php.ini giving the scripts much more time for the updates. For this you'd need to have access to the server's configuration. If you don't you could install a server pack (like XAMPP) on your local machine, there do the update operation and then move the updated DB to you production server.

There are other solutions for changing that variable on remote servers but they are usually turned off the by admins.

| improve this answer | |
  • I have the SQL query too, but the problem is the SQL query does only 60% of what I need. It have to be this way. Is there any way to get first 100 posts, then 2nd 100 posts, then 3rd 100 posts, like that? – Harry May 9 '17 at 19:14
  • Yeah, I think another approach would be in parts. You could use the wp_options() (add_option(), get_option(), update_option()) for storing the progress - for instance the last post updated (if you'd have them ordered by id). But then you would have to refresh the page multiple times until the process ends. – Picard May 9 '17 at 19:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.