Support

Account

Home Forums Front-end Issues Multiple taxonomy terms, but only show one random result

Helping

Multiple taxonomy terms, but only show one random result

  • I have a Custom Post Type named Services that can have an associated Service Expert. There can be multiple Service Experts associated with a Service, but I only want to show one of them, and I need it to be random.

    In the documentation and the forum, I haven’t found a way to only grab one random selection from a Taxonomy field. Would anyone be able to give me a push in the right direction here?

    I’ve been quite successful in getting a lot of what I need done with ACF and I’m really pleased with the level of documentation and support here in the forums. Thanks for your help!

    As an example, this url is pulling all three of the Service Experts, but I only want one.

    <?php
          $queryObject = new WP_Query( 'post_type=people&posts_per_page=1&orderby=rand' );
          // The Loop!
          if ($queryObject->have_posts()) {
    
            while ($queryObject->have_posts()) {
    
            // get selected people
          $people = get_field('service_expert');
    
          if( $people )
          {
    
          foreach( $people as $person )
          { 
    
           // Shows just the one lucky person!
           $queryObject->the_post(); ?>
    
                <?php echo get_the_post_thumbnail( $person->ID, 'person-thumb' ); ?>
    
           <?php
             }
           }
    
           wp_reset_query(); ?>
    
        <?php
            }
          }
        ?>
  • Assuming the OPer has moved on after 2 years, this is for anyone looking for similar information.

    
    <?php
      // get selected people
      $people = get_field('service_expert');
      if ($people) {
        $person = array_rand($people);
        // the rest of this would depend on what type of field it is
        // I'm assuming a relationship field
        // and that you are returning a array of post object
        get_the_post_thumbnail($person->ID, 'person-thumb');
      }
    ?>
    
Viewing 2 posts - 1 through 2 (of 2 total)

The topic ‘Multiple taxonomy terms, but only show one random result’ is closed to new replies.