You are here: Home . Code, WordPress . WordPress Tutorial: Update all posts at once

WordPress Tutorial: Update all posts at once

WordPress only allows you to update certain fields (like author, status, tags) for several posts at once from the admin section of your site. What do you do though if you’d like to update fields the WordPress admin section won’t allow you to? What happens if, for example, you’d like to change the post titles of all posts in a particular category? This can be done using a bit of code I’ll walk you through here.

Update 08/08/2015: Changed the code based on feedback

If, for example, you’d like to add a prefix ‘Post Prefix’ to every post in category with ID x, you’d use the following code:

        $posts_to_update = get_posts('cat=x&showposts=1000');//Retrieve the posts you are targetting
        foreach ( $posts_to_update as $update_this_post ):
            $update_this_post->post_title = 'Post Prefix: '.$update_this_post->post_title;//The post field you are updating.
            wp_update_post( $update_this_post );
        endforeach;

Replace ‘x’ with the ID of the category you’d like to update. ’1000′ is the number of posts we’d like to update; change it to what works for you

Using this technique, you can update any field (post title, authors, category, etc) of your posts in bulk at a go. Modify the code above to suit your needs by changing:

1. The get_posts

This is what determines which posts you’d like to update in bulk. In the example above, we targeted posts in a category with ID x (cat=x) and we restricted it to the first 1000 posts (&showposts=1000). Change this according to which posts you’d like to update. A full list of parameters you can use and a few examples are shown here

e.g. to target posts by author ‘user_nicename‘:

$posts_to_update  = new get_posts( 'author_name=kakoma' );

More on get_posts() here: get_posts() details

2. The $post_update_array

This is the next bit you’ll need to update. Change this to match what field you’d like to update. In the case above, we are updating post titles so we modify $update_this_post>post_title by adding ‘Post Prefix’ to the current title. You can modify content, author, etc in the same way. All you need to know is the field name.

e.g. To modify your content instead, replace:

$update_this_post->post_title

with

$update_this_post->post_content = $update_this_post->post_content.' Awesome suffix added to every post';

For a full list of the fields you can modify, check here

Changes to code are done. On to…

Where to put the code

There are very many places this code can be put. In functions.php in your theme, in a plugin, in a template…putting it in a plugin though is the most appropriate way of doing things. To do this, we’ll need to add a bit more code above the lines we’ve discussed. Use this:

<?php
/**
  * Plugin Name:       Post Bulk Update
  * Description:       On activation, I'll updates certain fields of your posts at once. Can't wait!
  * Version:           1.0.0
  * Author:            Your name here
  * License:           GPL-2.0+
  * License URI:       http://www.gnu.org/licenses/gpl-2.0.txt
  *  */
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly 

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('cat=x&showposts=1000');//Retrieve the posts you are targeting
    foreach ( $posts_to_update as $update_this_post ):
           $update_this_post->post_title = 'Post Prefix: '.$update_this_post->post_title;//The post field you are updating.
           wp_update_post( $update_this_post );
    endforeach;
 }

}
endif;

return new MyPlugin_BulkUpdatePosts();

Put this code in a file (e.g. post_bulk_update.php), put that file in a folder, zip that folder and upload it as a plugin (Instructions here). You’ll see a plugin named ‘Post Bulk Update’. On activating the plugin, your changes will be made. Delete the plugin after use.

Let me know if you run into any issues….or if it works for you.

Using diligent minions, I bring these tutorials & tips to your inbox every week. Give a minion work today; add your email address here