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’ );