Winner of the Best WordPress Business Website - WP Honors 2011
Since 2004
 

Forums

 

Manuals

 

Videos

 

Live Support

 

WPMU.org

Random post script

Elite

Is there code somewhere out there to jump to a random post? I think it would be great for encouraging inter-blog browsing by visitors to offer this button/link/etc. I have the post indexer plugin running.

Thanks!

Everyday at WPMU DEV we help hundreds of WP Users. Become a member today to:

  • Download 250+ Premium Plugins and Themes
  • Get unlimited support from WordPress Experts
  • Get help with your WordPress or BuddyPress projects

Help & Support from WPMU DEV members and staff

Staff

Hiya!

Probably the easiest way to do this would be to use our Recent Global Posts widget here:
http://premium.wpmudev.org/project/recent-global-posts-widget

You could set the number of posts to 1 in the widget settings.

To get it to display a random post you'd need to edit widget-recent-global-posts.php. You could edit line 158 from
"site_posts ORDER BY post_published_stamp DESC LIMIT "
to
"site_posts ORDER BY RAND() LIMIT "

Doing that, the widget will display one random post from the site_posts database.

Hope this helps! :D

Keeper of the Dark Chocolate

Also depends on how you're planning on presenting this random post. Are we just talking about a static link that drops you into a random post, a display of the_excerpt of a random post like along the sidebar, or (ah heck, I forget the third type.)

If you search the wp.org /extend/plugins database for random, you should see a couple of different plugins that do random posts in different manners.

I;d give some links but I'm stuck on a library computer with no copy/paste and only a single browser pane.

Get personal, comprehensive and timely support and assistance
from WordPress Experts

Elite

That's a really cool idea about modifying the random post widget. I hadn't thought about including that in our blogger's child theme.

For the question of the intended implementation, I had imagined on our front page putting a link that literally said "Random post >>" as an alternative to click instead of "All blogs >>" (I think the missing piece for drmike was this was network-wide, not blog wide). As such, I don't think the older WP plugins will work since it would need to pull from the post indexer table.

Essentially, the code would need to pick a random post from the table and return it as the url.

Staff

@teachforus,

Heh, simple solutions are where it's at!

To get the output as a link to a blog rather than an actual post itself, you could further modify the widget. Or write a custom query tapping into the Post Indexer plugin. (That's probably the solution you're interested in).

I'm not specifically a developer, but I took a crack at this and was able to customize (read: possibly ruin forever) Andrew's code from the Widget plugin and create a "Random Post" link. I inserted the following code into the loop.php file for twenty-ten.

Your mileage most definitely will vary and someone is welcome to come along and say "hah. try this instead." but hey - It's a start!

<?php $query = "SELECT * FROM " . $wpdb->base_prefix . "site_posts
ORDER BY RAND() LIMIT 1 ";
	$posts = $wpdb->get_results( $query, ARRAY_A );
	if (count($posts) > 0){
		foreach ($posts as $post){
			echo ' <a href="' . $post['post_permalink'] . '">' .
__('Random Post') . '</a>';
		}
	} ?>

Thanks and good luck!

*Edit* Ugh, the code formatting is giving me grief. If you have trouble copy/pasting this, let me know and I'll try to get this done line-by-line.

Staff

Or better yet, I'll just upload the code in a txt file here.

Thanks!

Elite

Cool. I'll check this out over the weekend.

Get personal, comprehensive and timely support and assistance
from WordPress Experts

We can help you out. Become a member today to:

  • Let our team help you out today and access to over 250+ Premium Plugins and Themes
  • Find out why people saying "it's like having your own developer"
  • WPMU DEV - The WordPress Experts