Random Posts on your WordPress homepage

Random Posts in WordPress

WordPress by default displays posts in date order with Ascending or Descending options. What if you prefer to show random posts on your homepage so that every time someone visits your site, they see a different selection of posts.

First off, why would you want to do this. It could be that older posts on your site are just as relevant as newer posts and they don’t get read because they are buried too deep due to their date. While there are a number of plugins that offer this functionality, they usually offer this for widgets only. But what if I told you that you can do this with five lines of code.

Copy the code below and paste into your Child theme’s function file. That’s it.

add_action('pre_get_posts','alter_query');
function alter_query($query){
if ($query->is_main_query() && is_home())
$query->set('orderby', 'rand'); //Set the order to random
}

Bulk change post dates by category

Post Date Time-Change

Recently I needed a plugin that changed the date of WordPress posts by category for a travel photo website. The site was divided into categories that were countries containing 1000 posts. The problem was these posts were not the same date as the photos as they were created years after taking them, So I wanted a way to choose change all the posts that resided in a category/country at the same time rather than change the date of each post individually. The solution turned out to be very easy. Watch the video for the plugin called Post Date Time Change. I have placed a link to the plugin below the video.

Post Date Time Change

Download Post Date Time Change →

NOTE: This plugin has one downfall. If you change multiple posts to the same timestamp, then it screws up post navigation because that is based on date/time in ascending or descending order. The solution is to change the seconds on the timestamp for each post manually. You can do this quite quickly using the same plugin. Go through all your affected posts and change the last two digits in the new date field that the plugin adds. These two digits are seconds. So start with say 59, then 58 for the next post and so on. Now each post with the same date will have a slightly different time. Consequently, this will restore normal functionality for post navigation when you click the ‘Next’ or ‘Previous’ links.