WP_Query with Advanced Custom Fields Meta Query Doesn’t Work

I have a WP_Query that builds a Bootstrap carousel. I have added an Advanced Custom field radio button that will allow the client a Yes/No option on the post to “Feature” this. My WP_Query works perfectly without the Meta Query, but when I add it in, it gives no results. I’m not sure if this is because this is on an archive.php or not. I have added in a dynamic category that will only show the featured posts from the current category (which is working perfectly also). It’s just the ACF that doesn’t seem to be working. I’ve verified that both the key & value are being stored in the database exactly like is being called here. I’ve even successfully echoed the value using a get_field() statement to make sure that was working. I’m stumped. Any suggestions would be greatly appreciated.

Here’s my code:

<?php
    $qcatid = get_queried_object(); // So we can get the query category ID
    $args2=array(
      'post_type' => 'post',
      'post_status' => 'publish',         
      'cat' => $qcatid->cat_ID, // Query the proper category
      'orderby' => 'date',          
      'posts_per_page' => -1,
      'meta_query' => array(              
          array(
            'key' => 'feature_in_slider_carousel',
            'value' => 'Yes'
          )
        )

    );
    $mycat_query = null; 
    $mycat_query = new WP_Query($args2);
    if( $mycat_query->have_posts() ) { 
    $slide_count = 0;
?>
<section id="featured-posts">
    <div class="container">
        <div class="row">
            <div class="col-md-12">
                <hr />
            </div>
        </div>
    </div>
    <div id="featured-carousel" class="carousel slide" data-ride="carousel">                  
      <div class="carousel-inner" role="listbox">
<?php
  while ($mycat_query->have_posts()) : $mycat_query->the_post(); ?>
    <div class="item <?php if ($slide_count == 1) { echo 'active';} ?>">
        <div class="row">
            <div class="col-sm-2 col-sm-offset-3">
                <div class="whitepaper-img">
                <a href="<?php the_permalink(); ?>">
                    <?php
                        include( TEMPLATEPATH . '/inc/icon-include.php' );
                        if (has_post_thumbnail( $post->ID ) ) {
                        $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'whitepaper-carousel' ); ?>
                        <img src="<?php echo $image[0]; ?>" alt="<?php the_title(); ?>" class="img-responsive" />
                    <?php } 
                        else { ?>
                        <img src="<?php bloginfo('template_directory'); ?>/img/default-whitepaper.png" alt="<?php the_title(); ?>" class="img-responsive" />    
                    <?php } ?>
                    </a>
                </div>
            </div>
            <div class="col-sm-5">                  
                <h3><a href="<?php the_permalink(); ?>"><?php the_title();?></a></h3>
                <?php the_excerpt();?>
                <div class="post-tags">
                    <?php
                        $posttags = get_the_tags();
                        if ($posttags) {
                            echo '<ul class="list-inline">';
                          foreach($posttags as $tag) {
                            echo '<li><a href="'. get_bloginfo('url') .'/tag/' . $tag->slug . '/">' . $tag->name . '</a></li>'; 
                          }
                          echo '</ul>';
                        }
                    ?>
                    </div>
            </div>
        </div>

    </div>
  <?php 
  $slide_count++;
  endwhile; ?>
  </div>

  <!-- Controls -->
  <a class="left carousel-control" href="#featured-carousel" role="button" data-slide="prev">
    <span class="glyphicon glyphicon-chevron-left" aria-hidden="true"></span>
    <span class="sr-only">Previous</span>
  </a>
  <a class="right carousel-control" href="#featured-carousel" role="button" data-slide="next">
    <span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span>
    <span class="sr-only">Next</span>
  </a>

</div>
    <div class="container">
        <div class="row">
            <div class="col-md-12">
                <hr />
            </div>
        </div>
    </div>
</section>
<?php  }
  wp_reset_query();
?>

Related posts

1 comment

  1. As far as I am able to tell your code is fine. I tested with a local site running on VVV and populated with WP dummy content.

    To test I created the custom field exactly as you described, with the possible exception being that I set the radio button value/label pairs up as:

    yes : Yes
    no : No
    

    I set the default value as no.

    I used a plugin called “Custom Field Bulk Editor” to bulk assign values to existing posts, then set up an archive.php using (mostly) your code and everything worked as expected.

    $qcatid = get_queried_object();
    
    $args2       = array(
      'post_type'      => 'post',
      'post_status'    => 'publish',
      'orderby'        => 'date',
      'cat'            => $qcatid->cat_ID,
      'posts_per_page' => -1,
      'meta_query'     => array(
        array(
          'key'   => 'feature_in_slider_carousel',
          'value' => 'Yes'
        )
      )
    );
    
    $mycat_query = null;
    $mycat_query = new WP_Query($args2);
    
    if ($mycat_query->have_posts()) :
      echo '<ul>';
    
      while ($mycat_query->have_posts()) : $mycat_query->the_post();
    
        echo '<li>' . get_the_title() . '</li>';
    
      endwhile;
      wp_reset_postdata();
      echo '</ul>';
    endif; 
    

    It’s worth noting that this also worked without meta_query :

    $args2 = array(
      'post_type'      => 'post',
      'post_status'    => 'publish',
      'orderby'        => 'date',
      'cat'            => $qcatid->cat_ID,
      'posts_per_page' => -1,
      'meta_key'       => 'feature_in_slider_carousel',
      'meta_value'     => 'Yes'
    );
    

    Visiting category archive pages in both cases only returned posts marked yes, and from the appropriate category.

    I’ve run into problems with ACF label/values in the past after having done some sort of edit to them, like changing foo : Bar to foo : Bat. When this has happened in the past I’ve found that deleting and re-creating the field with a different name has got things working for me.

    Sorry I couldn’t be of more help.

Comments are closed.