Virus In Training


Random post redirect to a given category by mozey
January 21, 2008, 4:48 am
Filed under: blog | Tags: , , , ,

Okay, so matt’s plugin Random Redirect is really cool if you have a photo/coolstuff type blog and you want people to click on a button and be redirected to a random post. But what if, you have also a bunch of “side note” type posts that you do not want to consume your lunch time reader’s attention with? like updates, how good the last movie was?.

Here is a REALLY quick hack that will alter Random Redirect to show only the content of ONE category. Then, all you need to do is to make sure you create an “umbrella” category which contains all of your pictures/whatever and Random Redirect will look only in there.

– Edit -> random-redirect.php

– Set the variable, $random_id as such: ( Replace the wp_term_relationships.term_taxonomy_id is equal to your category id that you want the search to be done against).

$random_id = $wpdb->get_var( "SELECT wp_posts.ID FROM $wpdb->posts, wp_term_relationships WHERE wp_posts.post_type = 'post' AND wp_posts.post_password = '' AND wp_posts.post_status = 'publish' AND wp_posts.ID = wp_term_relationships.object_id AND wp_term_relationships.term_taxonomy_id = 1 ORDER BY RAND() LIMIT 1");

Save and DONE!

********************
update:
here is the full code part of the plugin, i tried copying and pasting the whole thing, but wordpress kept on chopping off some stuff!. I’ll put it for download somewhere if its really not working!.
********************

function matt_random_redirect() {
global $wpdb;
$random_id = $wpdb->get_var( "SELECT wp_posts.ID FROM $wpdb->posts, wp_term_relationships WHERE wp_posts.post_type = 'post' AND wp_posts.post_password = '' AND wp_posts.post_status = 'publish' AND wp_posts.ID = wp_term_relationships.object_id AND wp_term_relationships.term_taxonomy_id = 1 ORDER BY RAND() LIMIT 1");
wp_redirect( get_permalink( $random_id ) );
exit;
}

if ( isset( $_GET[‘random’] ) )
add_action( ‘template_redirect’, ‘matt_random_redirect’ );

Advertisement

5 Comments so far
Leave a comment

I just can’t get it to work.

I would love to see this as a new standalone plug-in for us code challenged people.

It would also be amazing if the plug-in worked in such a way that you could define in the link, which category(ies) a random post should be pulled from (including “all” as with Matt’s plugin.

Nice try though. Thanks for looking.

Cheers.

Comment by Diane

I hope this helps!.

Comment by mozey

Well … I get the plugin to work, after manually changing all of the quotations. However, it always seems to randomly show me the same post from within the chosen category. In other words … it keeps generating the same post. Hope I can get this to work because its great otherwise!

Comment by OphthalmologyVideoFan

oh!!, not much i can do about that.

Well, i suppose we can store the posts you have viewd so far in a cookie and then avoid them, But seriously thats dirty and un necessary,

Do you have little amount of posts? or good, 30+ ?

Comment by mozey

I finally got it working somehow … the first problem I ran into was that I copied directly from this webpage and the quotation marks and apostrophy marks had to be manually changed back to normal in the php file. (maybe you could make your version downloadable as a txt file?).

Thank you soooo much for this little snippet of code. I’ve been stumped by this problem for the past 4 days. The databased changes in these latest wordpress releases totally confuse me.

Thanks!

Comment by OphthalmologyVideoFan




Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s



%d bloggers like this: