Update Old WordPress Post Dates Automatically on a Schedule

It’s pretty trendy now to show the “Last updated:” date on your blog posts instead of the “Published:” time. It’s arguably better for SEO, as both users and search engines like to see you do some housekeeping from time to time.

The only downside is, if you aren’t doing that housekeeping, some of your posts may be displaying a super old “Updated” date.

So, I developed a simple PHP script to grab a random post and update it, therefore resetting the “Last modified” time:

require('/home/full/path/to/public_html/wordpress/wp-blog-header.php'); // replace this with full path to your WordPress directory
$the_query = new WP_Query( array ( 'orderby' => 'rand', 'posts_per_page' => '1' ) );
while ( $the_query->have_posts() ) : $the_query->the_post();

Copy this code and put it in a new file called something like “update-random-post.php”, then upload it to your server.

Finally, set up a cron job and run it as often as you’d like, either with an external cron like EasyCron.com or on cPanel.

For example, if you run this every 6 hours, it will randomly update about 28 posts per week. If you have 1,000 posts, every post on your site will be updated about every 8 months on average.

You can adjust the frequency or the selection arguments to whatever you’d like.

Update: If you want some variation in the time that this runs, I recommend adding this to the top of this PHP script:

sleep(rand(1,10800)); // sleep for a random amount of time between 1 and 10800 seconds (3 hours)

Cron timing is pretty rigid, so if you choose every 6 hours, you will see 1:48 AM, 7:48 AM, 1:48 PM, 7:48 PM, and so on. Adding a random sleep timer beforehand helps randomize this a bit.

Once your script is modified and uploaded, here is the cron command you can use to run it on a schedule:

wget -O - -q -t 1 --timeout=0 https://www.yourdomain.com/yourscript.php

What People Are Saying:

No comments yet. Be the first!

Leave a Reply

Copyright 2024, All rights reserved. Yadda yadda.