0

I need to change the published year on all of a custom post type from 2010, to 2011.

What is the quickest way to do it? I have 200 entries, so manual would take hours!!

3 Answers 3

2

Rajeev Vyas is more or less correct... personally I think the best way to do this is to make a tiny plugin that will update all your dates when the plugin is activated. After updating the dates... delete the plugin and keep in mind that if you activate the plugin again all your dates will change again.

So step by step:

First create a new php file and add the appropriate header info so that Wordpress recognizes it as a plugin. Save this to your /wp-content/plugins folder. Here is an example:

/* Plugin Name: Plus One Year
 * Description: Adds one year to the publish date of all published posts
 * Author: Your Name
 */

Now in this newly created file create a function and tie it into your plugin activation hook. This function will take care of updating all your post dates.

function plus_one_year_activate(){}

// Register Plugin Activation Hook
register_activation_hook(__FILE__, 'plus_one_year_activate');

Now write the 'plus_one_year_activate' function:

function plus_one_year_activate(){

    global $wpdb;

    $format = 'Y-m-d H:i:s'; // Format that wordpress uses to save dates

    // Using $wpdb grab all of the posts that we want to update

    $post_type = 'your_custom_post_type'; //change this to your custom post type
    $post_status = 'publish'; // we only want to change published posts
    $query = "SELECT ID, post_date, post_date_gmt FROM $wpdb->posts WHERE post_type = '$post_type' AND post_status = '$post_status'";
    $posts = $wpdb->get_results( $query );

    // This loop will handle the date changing using wp_update_post
    foreach( $posts as $post ){

        $id = $post->ID;

        // get the old dates
        $old_date = $post->post_date;
        $old_gmt_date = $post->post_date_gmt;

        // create the new dates and correctly format the new date before saving it to the database
        $new_date = date( $format, strtotime( '+1 year' , strtotime( $old_date ) ) );
        $new_date_gmt = date( $format, strtotime( '+1 year' , strtotime( $old_gmt_date ) ) );

        $new_values = array (
            'ID' => $id,
            'post_date' => $new_date,
            'post_date_gmt' => $new_date_gmt
        );

        wp_update_post( $new_values );

    }
    return;
}

Save and go activate this plugin. Once activated... all your dates will change. After activating, delete this plugin so you don't accidentally add another year to your dates (your changes will persist even after you delete the plugin). Hope this helped.

0

May be you have to write code in plugin file and run...

Use get_posts to get posts for your custom post type and then loop it. Inside loop use wp_update_post function to update post_date.

Here is reference for wp_update_post http://codex.wordpress.org/Function_Reference/wp_update_post

 $my_post = array();
  $my_post['ID'] = 37;
  $my_post['post_date'] = your updated date

// Update the post into the database
  wp_update_post( $my_post );
-1

have you considered doing it in the database (for example using phpmyadmin)?

1
  • considered it, but not sure how to change the year, and only the year, on all posts of a certain type.
    – Mild Fuzz
    Commented Jul 25, 2011 at 9:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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