Quick random WordPress post hack December 26, 2009

A friend of mine, Dima, was looking for a plugin that’ll make it possible to link to random post for his new site, shlat.im.

He told me over IRC that he couldn’t find any simple plugin to do it, and I replied with this quick hack:

<?php require('wp-load.php'); header('Location: index.php?p='.$wpdb->get_var('SELECT ID FROM '.$wpdb->posts.' WHERE post_type=\'post\' AND post_status=\'publish\' ORDER BY RAND() LIMIT 1'));

Just stick it in a rand.php file in the wordpress folder, and link to it. it doesn’t get any simpler than that.

Leave a Reply