I'm working on looping through some books for a custom post type with advanced custom fields. One of the ACF's is a checkbox, and I'm wanting to only show the values with this one checked. The current loop I'm using is:


$args = array (
    'post_type' => 'book',
    'posts_per_page' => '2'

$thequery = new WP_Query( $args );


<?php if (have_posts()) : while ( $thequery->have_posts() ) : $thequery->the_post(); ?>
  <?php if( get_field('book_featured') ): ?>
    <div class="sidebar-book-header">
        <img src="<?php the_field( 'book_cover' ); ?>" alt="">
        <a href="<?php the_field( 'buy_now_amazon' ); ?>" class="sidebar-button book-button" style="background-color: <?php the_field( 'book_color_highlight' ); ?>" target="_blank"><span>Buy<br>Now</span></a>

    <p class="sidebar-book-info"><?php the_field( 'book_excerpt' ); ?> <a href="<?php the_permalink(); ?>" style="color: <?php the_field( 'book_color_highlight' ); ?>">Read More.</a></p>
  <?php endif; ?>
<?php endwhile; endif; ?>

This displays the first value, but doesn't include a second one (there are two that I have checked). What am I doing wrong that causes the loop to only show one?

closed as off-topic by Robert hue, Pieter Goosen, Mayeenul Islam, Wyck, Mark Kaplun Feb 18 '15 at 12:52

This question appears to be off-topic. The users who voted to close gave this specific reason:

If this question can be reworded to fit the rules in the help center, please edit the question.

  • Please refer to the documentation. BTW, support for specific plugin is off-topic. – Nilambar Feb 16 '15 at 3:20

The trouble is you're filtering at the loop stage, not the query. Here's what's happening:

  • WP_Query, give me the latest 2 books
  • Loop over them
  • Only one has book_featured, so only that shows

Instead, remove the if ( book_featured ) condition from your loop & add it to your query:

$args = array(
    'post_type' => 'book',
    'posts_per_page' => '2',
    'meta_key' => 'book_featured',
    'meta_value' => '1', // I'm not 100% sure this is how ACF stores checkbox data, you'll need to check the `wp_postmeta` table

I switched to a radio button instead of checkbox and used the meta wp_query option as described here and it worked!



I see an error in your code that can be the problem. This line:

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

should be:

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

Also, you should use wp_reset_postdata() after the loop from your custom query.

Not the answer you're looking for? Browse other questions tagged or ask your own question.