WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Pull a random category (8 posts)

  1. mjritter
    Member
    Posted 5 years ago #

    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!

  2. MichaelH
    Member
    Posted 5 years ago #

    <?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().
    
    ?>
  3. mjritter
    Member
    Posted 4 years ago #

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

  4. MichaelH
    Member
    Posted 4 years ago #

    Yes--or fit the ideas into your existing Template.

    Related:
    Stepping Into Template Tags
    Stepping Into Templates
    Template Hierarchy

  5. chuckie2455
    Member
    Posted 4 years ago #

    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

  6. MichaelH
    Member
    Posted 4 years ago #

    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().
    ?>
  7. chuckie2455
    Member
    Posted 4 years ago #

    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.

  8. radiofranky
    Member
    Posted 4 years ago #

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

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags