Title: Modifying Matt&#8217;s Random Post Plugin
Last modified: August 18, 2016

---

# Modifying Matt’s Random Post Plugin

 *  [umair](https://wordpress.org/support/users/umair/)
 * (@umair)
 * [18 years, 7 months ago](https://wordpress.org/support/topic/modifying-matts-random-post-plugin/)
 * I want to modify [Matt’s Random Post Plugin ](http://wordpress.org/extend/plugins/random-redirect/)
   so that it displays post from a particular category?
 * This is the proginal query present in the plugin:
 *     ```
       function matt_random_redirect() {
       	global $wpdb;
       	$random_id = $wpdb->get_var( "SELECT ID FROM $wpdb->posts WHERE post_type = 'post' AND post_password = '' AND post_status = 'publish' ORDER BY RAND() LIMIT 1");
       	wp_redirect( get_permalink( $random_id ) );
       	exit;
       }
   
       if ( isset( $_GET['random'] ) )
       	add_action( 'template_redirect', 'matt_random_redirect' );
   
       ?>
       ```
   
 * I am adding `AND post_category='2'` in `$random_id`
    but it start displaying 
   only the same post again n again and donot randomize 🙁 ??

Viewing 9 replies - 1 through 9 (of 9 total)

 *  Thread Starter [umair](https://wordpress.org/support/users/umair/)
 * (@umair)
 * [18 years, 7 months ago](https://wordpress.org/support/topic/modifying-matts-random-post-plugin/#post-613657)
 * any one >?
 *  Thread Starter [umair](https://wordpress.org/support/users/umair/)
 * (@umair)
 * [18 years, 7 months ago](https://wordpress.org/support/topic/modifying-matts-random-post-plugin/#post-613697)
 * any one
    ?
 *  Thread Starter [umair](https://wordpress.org/support/users/umair/)
 * (@umair)
 * [18 years, 7 months ago](https://wordpress.org/support/topic/modifying-matts-random-post-plugin/#post-613702)
 * once again … can any one help me on this ?
 *  Thread Starter [umair](https://wordpress.org/support/users/umair/)
 * (@umair)
 * [18 years, 7 months ago](https://wordpress.org/support/topic/modifying-matts-random-post-plugin/#post-613708)
 * ??
 *  Thread Starter [umair](https://wordpress.org/support/users/umair/)
 * (@umair)
 * [18 years, 7 months ago](https://wordpress.org/support/topic/modifying-matts-random-post-plugin/#post-613721)
 * i am going to make a world record out of this ….
 *  Thread Starter [umair](https://wordpress.org/support/users/umair/)
 * (@umair)
 * [18 years, 6 months ago](https://wordpress.org/support/topic/modifying-matts-random-post-plugin/#post-613736)
 * do you have any idea how to solve it, if yes… please update.
 *  Thread Starter [umair](https://wordpress.org/support/users/umair/)
 * (@umair)
 * [18 years, 6 months ago](https://wordpress.org/support/topic/modifying-matts-random-post-plugin/#post-613742)
 * am i near?
 *  Thread Starter [umair](https://wordpress.org/support/users/umair/)
 * (@umair)
 * [18 years, 6 months ago](https://wordpress.org/support/topic/modifying-matts-random-post-plugin/#post-613753)
 * no … i think i am not … 🙁
 *  [alexleonard](https://wordpress.org/support/users/alexleonard/)
 * (@alexleonard)
 * [18 years, 2 months ago](https://wordpress.org/support/topic/modifying-matts-random-post-plugin/#post-613812)
 * Hi umair,
 * My php knowledge isn’t the best and this might be incorrect, so someone please
   correct me if I’m wrong, but I can’t ignore that many pleas for help!
 *     ```
       <?php
       function matt_random_redirect() {global $wpdb; $random_id = $wpdb->get_var( "SELECT ID FROM $wpdb->posts WHERE post_type = 'post' AND post_password = '' AND post_status = 'publish' AND $wpdb->term_taxonomy.term_id = 1,2,3 AND $wpdb->term_taxonomy.taxonomy = 'category' ORDER BY RAND() LIMIT 1"); 
   
       wp_redirect( get_permalink( $random_id ) ); exit; } if ( isset( $_GET['random'] ) ) add_action( 'template_redirect', 'matt_random_redirect' ); ?>
       ```
   
 * I may be missing out on LEFT JOIN or something more complicated. The problem 
   you’ve had is something to do with the fact that as this is a direct database
   query you can’t just use AND post_category = “2” as that’s not correct for $wpdb-
   >posts, I think…
 * There’s a good bit more information on this page:
 * [Displaying Posts Using a Custom Select Query](http://codex.wordpress.org/Displaying_Posts_Using_a_Custom_Select_Query)
 * Which might be fairly relevant (especially the example at the very end of the
   page).
 * There’s also more information here:
 * [Function Reference: wpdb_Class](http://codex.wordpress.org/Function_Reference/wpdb_Class)
 * Where you should look at the information on using get_var as this is what Matt
   has utilised for this plugin.
 * Hopefully that helps, again I could be completely wrong…

Viewing 9 replies - 1 through 9 (of 9 total)

The topic ‘Modifying Matt’s Random Post Plugin’ is closed to new replies.

 * 9 replies
 * 2 participants
 * Last reply from: [alexleonard](https://wordpress.org/support/users/alexleonard/)
 * Last activity: [18 years, 2 months ago](https://wordpress.org/support/topic/modifying-matts-random-post-plugin/#post-613812)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
