Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@kakoma
Last active June 27, 2017 06:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kakoma/21adbfef4696af65238c26705bbd0188 to your computer and use it in GitHub Desktop.
Save kakoma/21adbfef4696af65238c26705bbd0188 to your computer and use it in GitHub Desktop.
WordPress Plugin to update WordPress posts in bulk, changing any post field, not just the ones WP admin allows. Customize the code inside the foreach loop as desired. Save this in a file, place it in your plugins directory and activate it. The changes will run on activation. Delete the plugin after the change. Article on this here: http://kakoma…
<?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. CHANGE THIS to retrieve the posts you want to update
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();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment