Title: Display &#8220;get_pages&#8221; Array in Random Order?
Last modified: August 19, 2016

---

# Display “get_pages” Array in Random Order?

 *  Resolved [jeffmckeand](https://wordpress.org/support/users/jeffmckeand/)
 * (@jeffmckeand)
 * [16 years, 8 months ago](https://wordpress.org/support/topic/display-get_pages-array-in-random-order/)
 * Hello everyone.
 * I’ve written the code below to pull three testimonials from the portfolio section
   of a site and display them on the home page. Everything is working fine, except
   I can’t seem to get them to display in a random order.
 * Is there a way to do this using “get_pages”, or am I stuck always showing them
   in a specific order?
 * Here’s the code:
 *     ```
       <?php
   
       	$pages = get_pages(array('child_of' => 15, 'meta_key' => 'testimonial', 'number' => 3));
   
       	foreach($pages as $child) {
   
       		$testimonial = get_post_meta($child->ID, 'testimonial', false);
       		$projName = get_the_title($child->ID);
       		$projLink = get_permalink($child->ID);
   
       	if ($testimonial) {
   
       		foreach ($testimonial as $testimony) {
   
       			$fullValue = explode("|", $testimony);
   
       			$docPic = $fullValue[0];
       			$docQuote = $fullValue[1];
       			$docName = $fullValue[2];
   
       			?>
   
       			<li class="testimonial">
   
       				<img src="<?php echo $docPic; ?>" alt="<?php echo $docName; ?>" title="<?php echo $docName; ?>" />
   
       				<p class="quote">&ldquo;<?php echo $docQuote; ?>&rdquo;</p>
   
       				<p class="cite"><span class="doc"><?php echo $docName; ?></span> | <a href="<?php echo $projLink; ?>" title="<?php echo $projName; ?>"><?php echo $projName; ?></a></p>
   
       			</li><!-- /.testimonial -->
   
       			<?php
   
       		}
   
       	}
   
       	}
   
       ?>
       ```
   
 * Thanks for whatever help you can provide! 🙂

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

 *  [Mark / t31os](https://wordpress.org/support/users/t31os_/)
 * (@t31os_)
 * [16 years, 8 months ago](https://wordpress.org/support/topic/display-get_pages-array-in-random-order/#post-1214690)
 * After..
 *     ```
       $pages = get_pages(array('child_of' => 15, 'meta_key' => 'testimonial', 'number' => 3));
       ```
   
 * Add..
 *     ```
       shuffle($pages);
       ```
   
 * Does that help?
 *  Thread Starter [jeffmckeand](https://wordpress.org/support/users/jeffmckeand/)
 * (@jeffmckeand)
 * [16 years, 8 months ago](https://wordpress.org/support/topic/display-get_pages-array-in-random-order/#post-1214694)
 * It kind of works. It shuffles the results, but those results are still the first
   three pages (in alpha-by-title order) that the function finds.
 * I’m wanting the function to pull random results.
 * Can’t figure out how…
 *  Thread Starter [jeffmckeand](https://wordpress.org/support/users/jeffmckeand/)
 * (@jeffmckeand)
 * [16 years, 8 months ago](https://wordpress.org/support/topic/display-get_pages-array-in-random-order/#post-1214705)
 * Got it. I don’t know why I tried it with “get_pages”. Switched to “query_posts”,
   reconfigured the variables, added “‘orderby’ => rand”, and now it works just 
   fine.
 * Thanks anyway for your help t31os_.
 * Here’s the working code:
 *     ```
       <?php
   
       	$pages = query_posts(array('post_parent' => 15, 'post_type' => 'page', 'meta_key' => 'testimonial', 'showposts' => 3, 'orderby' => rand));
   
       	foreach($pages as $child) {
   
       		$testimonial = get_post_meta($child->ID, 'testimonial', false);
       		$projName = get_the_title($child->ID);
       		$projLink = get_permalink($child->ID);
   
       	if ($testimonial) {
   
       		foreach ($testimonial as $testimony) {
   
       			$fullValue = explode("|", $testimony);
   
       			$docPic = $fullValue[0];
       			$docQuote = $fullValue[1];
       			$docName = $fullValue[2];
   
       			?>
   
       			<li class="testimonial">
   
       				<img src="<?php echo $docPic; ?>" alt="<?php echo $docName; ?>" title="<?php echo $docName; ?>" />
   
       				<p class="quote">&ldquo;<?php echo $docQuote; ?>&rdquo;</p>
   
       				<p class="cite"><span class="doc"><?php echo $docName; ?></span> | <a href="<?php echo $projLink; ?>" title="<?php echo $projName; ?>"><?php echo $projName; ?></a></p>
   
       			</li><!-- /.testimonial -->
   
       			<?php
   
       		}
   
       	}
   
       	}
   
       ?>
       ```
   
 *  [Mark / t31os](https://wordpress.org/support/users/t31os_/)
 * (@t31os_)
 * [16 years, 8 months ago](https://wordpress.org/support/topic/display-get_pages-array-in-random-order/#post-1214708)
 * Good stuff…
    A good example of a user posting back their solution.
 * I was going to suggest get_posts, but if it’s the only thing happening on the
   page, then yes, just rewrite the parameters for query_posts…

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

The topic ‘Display “get_pages” Array in Random Order?’ is closed to new replies.

## Tags

 * [array](https://wordpress.org/support/topic-tag/array/)
 * [get_pages](https://wordpress.org/support/topic-tag/get_pages/)
 * [order](https://wordpress.org/support/topic-tag/order/)
 * [query_posts](https://wordpress.org/support/topic-tag/query_posts/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 4 replies
 * 2 participants
 * Last reply from: [Mark / t31os](https://wordpress.org/support/users/t31os_/)
 * Last activity: [16 years, 8 months ago](https://wordpress.org/support/topic/display-get_pages-array-in-random-order/#post-1214708)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
