Title: Pull a random category
Last modified: August 19, 2016

---

# Pull a random category

 *  Resolved [mjritter](https://wordpress.org/support/users/mjritter/)
 * (@mjritter)
 * [16 years, 3 months ago](https://wordpress.org/support/topic/pull-a-random-category/)
 * I am using this query to pull in posts from category “Video” :
 *  <?php $my_query = new WP_Query(‘category_name=Video’);
    while ($my_query->have_posts()):
   $my_query->the_post(); ?>
 * Is there a way I can change this query to have the posts be from completely random
   categories and not just the Video category?
 * Thanks!

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

 *  [MichaelH](https://wordpress.org/support/users/michaelh/)
 * (@michaelh)
 * [16 years, 3 months ago](https://wordpress.org/support/topic/pull-a-random-category/#post-1410669)
 *     ```
       <?php
   
       $taxonomy = 'category';
       $param_type = 'category__in';
       $term_args=array(
         'orderby' => 'name',
         'order' => 'ASC',
       );
       $terms = get_terms($taxonomy,$term_args);
   
       if ($terms) {
         $count = 0;
         $random = rand(0,count($terms)-1);  //get a random number
         foreach( $terms as $term ) {
           $count++;
           if ($count == $random ) {  // only if count is equal to random number display get posts for that category
             $args=array(
               "$param_type" => array($term->term_id),
               'post_type' => 'post',
               'post_status' => 'publish',
               'posts_per_page' => -1,
               'caller_get_posts'=> 1
               );
             $my_query = null;
             $my_query = new WP_Query($args);
             if( $my_query->have_posts() ) {
               echo 'List of Posts in '.$taxonomy .' '.$term->name;
               while ($my_query->have_posts()) : $my_query->the_post(); ?>
                 <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
                <?php
               endwhile;
             }
           }
         }
       }
       wp_reset_query();  // Restore global post data stomped by the_post().
   
       ?>
       ```
   
 *  Thread Starter [mjritter](https://wordpress.org/support/users/mjritter/)
 * (@mjritter)
 * [16 years, 3 months ago](https://wordpress.org/support/topic/pull-a-random-category/#post-1410910)
 * So I can basically just take out the query i had and put this in the place of
   it?
 *  [MichaelH](https://wordpress.org/support/users/michaelh/)
 * (@michaelh)
 * [16 years, 3 months ago](https://wordpress.org/support/topic/pull-a-random-category/#post-1410911)
 * Yes–or fit the ideas into your existing [Template](http://codex.wordpress.org/Templates).
 * Related:
    [Stepping Into Template Tags](http://codex.wordpress.org/Stepping_Into_Template_Tags)
   [Stepping Into Templates](http://codex.wordpress.org/Stepping_Into_Templates)
   [Template Hierarchy](http://codex.wordpress.org/Template_Hierarchy)
 *  [chuckie2455](https://wordpress.org/support/users/chuckie2455/)
 * (@chuckie2455)
 * [16 years, 1 month ago](https://wordpress.org/support/topic/pull-a-random-category/#post-1411031)
 * Thanks Michael, this code is a great little trick!
 * Using your code above, how could I limit the results to only show posts from 
   a random category that had AT LEAST 4 posts in it?
 * Much appreciated and thanks
 *  [MichaelH](https://wordpress.org/support/users/michaelh/)
 * (@michaelh)
 * [16 years, 1 month ago](https://wordpress.org/support/topic/pull-a-random-category/#post-1411032)
 * Okay had to fix a little something (it never actually displayed the very first
   term if that term was the random choice) and add your request:
 *     ```
       <?php
       $min_count=4; //minimum number of posts in term to consider
       $taxonomy = 'category';
       $param_type = 'category__in';
       $term_args=array(
         'orderby' => 'name',
         'order' => 'ASC',
       );
       $allterms = get_terms($taxonomy,$term_args);
   
       // only terms that meet our $min_count
       if ($allterms) {
         $terms=array();
         foreach( $allterms as $term ) {
           if ($term->count >= $min_count) {
            $terms[]=$term;
           }
         }
       }
   
       if ($terms) {
         $random = rand(0,count($terms)-1);  //get a random number from 0 to number of elements in $terms array
         $term=$terms[$random];
         $args=array(
           "$param_type" => array($term->term_id),
           'post_type' => 'post',
           'post_status' => 'publish',
           'posts_per_page' => -1,
           'caller_get_posts'=> 1
           );
         $my_query = null;
         $my_query = new WP_Query($args);
         if( $my_query->have_posts() ) {
           echo 'List of Posts in '.$taxonomy .' '.$term->name;
           while ($my_query->have_posts()) : $my_query->the_post(); ?>
             <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
            <?php
           endwhile;
         }
       }
       wp_reset_query();  // Restore global post data stomped by the_post().
       ?>
       ```
   
 *  [chuckie2455](https://wordpress.org/support/users/chuckie2455/)
 * (@chuckie2455)
 * [16 years, 1 month ago](https://wordpress.org/support/topic/pull-a-random-category/#post-1411034)
 * WOW, thanks Michael for adding the request and fix – this piece of code is very
   useful and I know will come in handy for many wordpress developers out there –
   cheers.
 *  [radiofranky](https://wordpress.org/support/users/radiofranky/)
 * (@radiofranky)
 * [16 years ago](https://wordpress.org/support/topic/pull-a-random-category/#post-1411046)
 * hi,
    I have this code in my home.php to display category + category icon. i was
   wondering if is possible to select the category randomly?
 *     ```
       <?php recent_posts('limit=10&included_cats=538'); ?>
       <?php get_cat_icon('link=true&class=myicons&cat=538&description=true'); ?>
       ```
   
 * tried this but something is not right..
 *     ```
       <?php
       $min_count=4; //minimum number of posts in term to consider
       $taxonomy = 'category';
       $param_type = 'category__in';
       $term_args=array(
       'orderby' => 'name',
       'order' => 'ASC',
       );
       $allterms = get_terms($taxonomy,$term_args);
   
       // only terms that meet our $min_count
       if ($allterms) {
       $terms=array();
       foreach( $allterms as $term ) {
       if ($term->count >= $min_count) {
       $terms[]=$term;
       }
       }
       }
   
       if ($terms) {
       $random = rand(0,count($terms)-1); //get a random number from 0 to number of elements in $terms array
       $term=$terms[$random];
       $args=array(
       "$param_type" => array($term->term_id),
       'post_type' => 'post',
       'post_status' => 'publish',
       'posts_per_page' => -1,
       'caller_get_posts'=> 1
       );
       $my_query = null;
       $my_query = new WP_Query($args);
       if( $my_query->have_posts() ) {
       recent_posts('limit=10&included_cats=$term');
   
       }
       }
       wp_reset_query(); // Restore global post data stomped by the_post().
       ?>
       ```
   

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

The topic ‘Pull a random category’ is closed to new replies.

## Tags

 * [meh_code](https://wordpress.org/support/topic-tag/meh_code/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 7 replies
 * 4 participants
 * Last reply from: [radiofranky](https://wordpress.org/support/users/radiofranky/)
 * Last activity: [16 years ago](https://wordpress.org/support/topic/pull-a-random-category/#post-1411046)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
