Support » Fixing WordPress » Pull a random category

  • Resolved mjritter

    (@mjritter)


    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

    (@michaelh)

    <?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

    (@mjritter)

    So I can basically just take out the query i had and put this in the place of it?

    MichaelH

    (@michaelh)

    Yes–or fit the ideas into your existing Template.

    Related:
    Stepping Into Template Tags
    Stepping Into Templates
    Template Hierarchy

    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

    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().
    ?>

    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.

    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.