preload

Randomize the Dates of WordPress Posts

Posted by ctrtard on Apr 19, 2010

I wrote this script after I built my first WordPress datafeed site.   After I loaded up the site, I realized that I had 5,000+ posts all made on the same day!   Rather than repost everything, I wrote this quick script.  Hopefully someone may find it useful.

What it Does

This script will connect to your WordPress database and change the post dates on all of your posts to random dates.  You can define the range of dates by setting the two variables: $min_days_old, and $max_days_old.   For example: if you set $min_days_old = 1 and $max_days_old = 30, the script will go through and change all of the post dates to be within the past 30 days.

How to Use It

Edit the MySQL connection info at the top. (Also be sure to change $wp_table to the name of your WordPress posts table if it is different.)  Next, set the min and max days old.  Now all you need to do is visit the script URL in your browser.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
<?php
// define mysql connect
$dbname = "name";
$dbuser = "user";
$dbpass = "pass";
$dbhost = "localhost"; // this can usually stay 'localhost'
 
$wp_table = "wp_posts"; // define wordpress table name  
 
$gmt_offset = '-8'; // -8 for California, -5 New York, +8 Hong Kong, etc.
 
$min_days_old = 1; // the minimum number of days old
$max_days_old = 90; // the maximum number of days old
 
// connect to db
mysql_connect($dbhost,$dbuser,$dbpass);
mysql_select_db($dbname);
 
 
$result = mysql_query("SELECT ID FROM $wp_table WHERE post_type = 'post'") or die(mysql_error()); 
while ($l = mysql_fetch_array($result)) {
    $post_id = $l['ID'];
    echo "Updating: $post_id <br>";
 
    $day = rand($min_days_old, $max_days_old);
    $hour = rand(0, 23);
 
    $new_date = date( 'Y-m-d H:i:s', strtotime("-$day day -$hour hour") );  
    $gmt_new_date = date( 'Y-m-d H:i:s', strtotime("-$day day -$hour hour -$gmt_offset hour") );
 
    mysql_query("UPDATE $wp_table SET post_date='$new_date', post_date_gmt='$gmt_new_date',
    post_modified='$new_date', post_modified_gmt='$gmt_new_date' WHERE ID='$post_id'")
    or die(mysql_error()); 
 
}
 
echo "<hr>DONE!";
 
?>

Enjoy!

0saves
If you enjoyed this post, it's easy to thank me! Share it using a button on the left, leave a comment or subscribe to my RSS feed. Thanks!

Related posts:

  1. How To Use WordPress as a Landing Page

Tags: , , ,
  • 23 responses to "Randomize the Dates of WordPress Posts"

  • dom
    August 24, 2010 8:47 PM 

    worked great thanks a lot!!!

  • Matt
    September 16, 2010 7:48 PM 

    This is brilliant. I had a couple of old blogs that I wrote a lot of random junk on back in 2008. I hadn’t touched much since. I just ran this script and now it looks like they’re current.

    Nice work on the coding.

  • Norbert
    November 10, 2010 8:44 AM 

    Hi…

    Thanks for that, saved me really some time. One thing could be interesting, to make a JOIN in the SQL statement, so you can call for changes on only one category.

    Thanks

    • ctrtard
      November 13, 2010 12:37 PM 

      Good idea, it would make it handier for sure. If you ever code that up I’d be happy to post it.

      Thanks for reading!

  • xgarrettx
    November 10, 2010 11:56 AM 

    holy shit, this is was the perfect workaround for a few sites im developing. thanks man!

  • Dorian
    November 12, 2010 2:37 AM 

    WOW! Thanks for this awesome script. Worked like magic on a 50,000 page site! :-)

  • Jeff
    November 14, 2010 7:58 PM 

    Perfect!! I imported just over 28,000 records into WordPress and the plan was to write my own function next weekend for the dates, but… yours worked fabulously. Took a little over a minute but gave me 6 months of posts and couldn’t have been easier. Thanks for sharing!

  • Mridul
    December 09, 2010 10:08 AM 

    Dude …..
    you are Awesome ….
    you saved my 8 months of work….

    :-) and a hug to you

    Great work and keep it up

    Cheers

  • Austin
    December 22, 2010 10:00 AM 

    Hey – pretty much what i was looking for. What extra code would be needed to restrict it to a specific category?

    Thanks!!

    • ctrtard
      December 29, 2010 8:33 PM 

      Off the top of my head, I’m not sure. If I remember correctly categories are set according to their own id’s. The posts, in turn, reference these id(s). Have a look at your WP db using phpMyAdmin and you’ll see what I mean. The mod wouldn’t be hard, it would just be a matter of modifying the SQL query.

      Thanks for reading & commenting!

  • henri
    May 22, 2011 5:48 AM 

    Nice piece of code! Perhaps you can make a variant for updating the post_modified and post_modified_gmt date so that you tell the world that you update those posts. A random of 50 posts per day will be fine I think.

    • ctrtard
      July 01, 2011 4:17 AM 

      If I understand you right, you don’t want to modify the post date, but ONLY the post_modified date? You can do that easily by remove the post_date= and post_date_gmt= portions of the SQL query on line 31.

      Thanks for reading!

  • Alex
    June 28, 2011 7:24 PM 

    this is just perfect, only one question tho, is it possible to run it for a range of posts and not all the posts?

    • ctrtard
      July 01, 2011 4:12 AM 

      Hi, yes you can do that pretty easily. The trick is to modify they SQL query on line 20. Right now the query looks like this:

      SELECT ID FROM wp_posts WHERE post_type = ‘post’

      Supposed I want to only update posts that are between Jan 1 and Apr 1 of 2011. I could change the query to look like this:

      SELECT ID FROM wp_posts WHERE post_type = ‘post’ AND post_date > ’2011-01-01 00:00:00′ AND post_date < ’2011-04-01 00:00:00′

      Note, the date is international format. Meaning: Year-Month-Day. The time is in 24-hour format.

  • KJ Ross
    September 27, 2011 12:48 PM 

    Is it possible to do this for comments instead of posts?

  • Leave a Comment

    * Required
    ** Your Email is never shared
    *

    CommentLuv badge