Post Pic

How to: Randomize posts order

Are you building a portfolio or a gallery with WordPress? If yes, it can be a good thing to randomize posts order, just like I did on the footer of WpRecipes.com theme to display WordPress themes. That’s very easy to do with WordPress: Just read on.

To randomize posts order, you'll have to use the very powerful query_posts() function before your WordPress loop:

query_posts('orderby=rand');
//Your loop goes here

Of course, it is also possible to randomize only a certain category:

query_posts('cat=10&orderby=rand');
//Your loop goes here

11 Responses

Feb 02 2009 16:46

Great suggestion. Thanks for the tip!

Feb 02 2009 18:02

It works fine on parent category but when viewing child/sub category it shows all post from parent (meaning all sub category posts). Suggestions? Thanks.

Feb 03 2009 17:35

Great recipe, as always!

Feb 06 2009 03:09

Great recipe here. It’s going to really help with this theme idea I’ve had for a while. Thanks a load. Didn’t even think this was possible.

Feb 27 2009 00:41

Thanks for that, great bit of code!!

May 25 2009 06:39

Hey!

Just wondering if you are able to add this into a category page? I tried it, but it displayed all categories on a page, not the category I was on.

I see you put that you can add the cat id to the query, but I have at least 30 categories, so wondering if there is a quicker way to do it instead of doing conditional tags for each category.

Thanks
Stacey

Nov 12 2009 01:28

thanks for this solution. I have specific theme and sometimes I’ve troubles with some function, for example some plugins doesn’t work, but I’ll try use this code.

Thanks
Dekoracje Tort

Trackbacks:

Leave a Comment

* Name, Email, Comment are Required