Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

General Discussion

Caio Ferrari
Caio Ferrari
5,455 Points

Randomize array content in PHP via Wordpress posts

Do you guys know how to limit posts per page and randomize posts in wordpress?

I have a relationship field in the back-end where I add and remove items that I created to display in a website. This content is printed through the WP_Query below:

  $args = array (
    'post_type'      => 'home_banners'

  $fullbanner = new WP_Query ( $args );

And here is the PHP:

<?php if ( have_posts() ) : while ( $fullbanner->have_posts() ) : $fullbanner->the_post(); ?>

#####Get the relationship field
<?php $banners = get_field('home_banner_01_selection'); ?>
#####Check if the relationship field has contents
<?php if( $banners ): ?>
  #####Start foreach
  <?php foreach( $banners as $banner ): ?>
    <a href="<?php the_field('home_link', $banner->ID ); ?>"><?php the_field( 'home_headline', $banner->ID ); ?></a>
  <?php endforeach; ?>
<?php endif; ?>

I have three contents added in this relationship field. I want just to display ONLY ONE content per page and randomize it when refreshing the page. At the moment it is currently display all the three contents in the page.

I notice that the var $banners behave as an array. If I add echo count($banners); it will display 3. Moreover, if I add shuffle($banners); it will shuffle the content among them.

Thanks for helping me.

4 Answers

inside your arguments at the top.

$args = array(
  'post_type' => 'home_banners',
  'posts_per_page' => 1,
  'orderby' => 'rand'
$fullbanner = new WP_Query( $args );

NOTE: The use of commas and how the last item in the argument DOES NOT have a comma. Also dont forget your closing parenthesis and semicolon to end the argument array.

That code will only give you 1 post from that post type, if there are multiple posts it will be random each time.

Reference for both was from the same page of the Wordpress Codex.

Pagination for limiting posts

Order and OrderBy usage

Caio Ferrari
Caio Ferrari
5,455 Points

Hi Chris Howell

Thanks for helping!

Yeah, that is exactly what I've been making all the time, but I noticed that relationship fields cannot be randomized or limited. In general, the WP_Query is not working. I used the "post_count" parameter and it is printing "1" in the page. So thats why I am requesting and appealing the array shuffle or array rand method! Why WP_Query doesn't want to obey me??

Give me a little bit and I will get back to you. I am going to see if I can't go pull some of my wordpress code for a site I made for someone awhile back, I bent wordpress to my will on that site. Made it do all sorts of fun things. Think I may have had some code similar to what you are trying to do.

Can you tell me what that relationship is made up of "behind the scenes"?

the one labeled "home_banner_01_selection" like what custom fields and field types are they made up of?