wordpress snippets last updated   : July 14, 2014
we now have 634 snippets

Display posts in a random order, but retain persistent pagination

wordpress snippet

Randomising the order of the returned posts in a WordPress query is easy – simply add 'orderby' => 'rand' to the arguments and you’re good to go – this is great, until you need to paginate the results. As soon as you navigate to the second page of the results, the posts are returned randomly once again and you get an entirely different set of posts that may or may not contain posts that you have already viewed – if you’ve experienced this before then you’ll know exactly how frustrating it can be. The first thing we need to do is make sure our PHP session is initiated and then we can use the 'posts_orderby' filter to handle the persistent randomisation.

read more

Redirect home page to a random blog post

wordpress snippet

This is a slight mod of the wordpress post from yesterday (Redirect home page to first blog post) and think you might enjoy it. Add this snippet to the index.php template of your wordpress theme before get_header(). Your home page will automatically redirect to a random post from your wordpress database.

read more

Random blog post

wordpress snippet

Adding this PHP code to your wordpress theme just before the loop will display one random post from your wordpress database.

read more
jqsnipp.com | we just launched jquery snippets website