get random posts the easy way

I’m currently writting a very cool plugin/addon for wordpress. Therefor I needed a way to get a defined number of random posts.
And guess what? There is already a function providing this feature: get_posts()

To get 4 random posts just use this function call:

get_posts(‘numberposts=4&orderby=rand()’);

Since the argument orderby will be forwarded to the SQL statement, we will get 4 random posts from database. There are several more arguments, which can be found in the codex.

Leave a Reply