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

WordPress How to Build a WordPress Theme Extending WordPress Template Functionality Adding Content in Custom Places: Part 1

Caio Ferrari
Caio Ferrari
5,455 Points

Limit posts and randomize items from a Relationship Field

For my Wordpress website, I'm using Advanced Custom Fields. I have two Custom Post Types - one is "Banners" (the_banners) and the other one is "Content" (add_content). For the "Content" CPT I created a taxonomy called "Home Banners" (add_home_banner). For every new content, I always checkmark the "Full Banner" category from the Home Banners' taxonomy.

When I click on the CPT "Banners" I have a relationship field (banner_selection) that pulls all the "Full Banners" category that I marked when adding the content.

My main question is: I would like to select several items in the relationship field. However, I'd like only to display one post per page and randomize it when refreshing the page. I've made the following code!

<?php
    $ids = get_field('banner_selection', false, false);
    'post_type'      => 'the_banners',
    'posts_per_page' => 1,
    'post__in'       => $ids,
    'post_status'    => 'any',
    'orderby'        => 'rand',
  ?>

  $fullbanner = new WP_Query ( $args );

  <?php if ( have_posts() ) : while ( $fullbanner->have_posts() ) : $fullbanner->the_post(); ?>
  <?php
    $banners = get_field('banner_selection');
  ?>
  <?php if( $banners ): ?>
    <?php foreach( $banners as $banner ): ?>
      //code...

Teachers! Please, help me!

Thank you!

2 Answers

Sorry Am an idiot just reread your code, need to stop posting when half asleep

Caio Ferrari
Caio Ferrari
5,455 Points

Well, I tried the code you post and I keep stuck. =(

How's gonna save me?