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

John Locke
John Locke
15,479 Points

Filter Posts in WP loop by Custom Field (ACF)

This one has been giving me trouble all day. I'm using the Advanced Custom Fields plugin to create a checkbox on the Publish/edit posts screen.

This is what I'm trying to accomplish:

If the check box is checked, The post is added to a Loop on the sidebar.

What happens now is that the custom field is ignored, and the most recent posts are displayed, the custom field checkbox is ignored. I've tried dozens of variations on this. I need to fix the initial query to begin with. I need to know the correct way to query posts with a checkbox with a name of 'favorites' and a value of 'yes'. (I tried true/false custom field, also didn't work) The basic gist of the query:

$args = array (
    'post_type' => 'post',
    'showposts' => 12,
     'meta_query' => array (
          array (
             'key' => 'favorites',
             'value' => 'yes'
          )
        )
      )
      $myquery = new WP_Query($args);

Looked at all different angles to this on ACF do umentation, Stack Overflow, WP sites, and the Codex. Tried using the True/False custom field with key, value of 1, and compare of ==. No luck. Tried variations on get_posts, WP_Query, the loop. Something I am not seeing in the query or the args. Thanks Zac Gordon and other WordPress devs.

John Locke
John Locke
15,479 Points

Just so you know, I see the missing parentheses in the array. I'm in bed, Trying to type this on my iPhone. I can't edit it easily.

4 Answers

Szabolcs Légrádi
Szabolcs Légrádi
9,797 Points

It seems like you are missing the closing parenthesis of meta_query. Here is a modified version. Note that I also removed the post_type line, since it defaults to 'post' anyways.

$args = array (
    'showposts' => 12,
    'meta_query' => array (
        array (
            'key' => 'favorites',
            'value' => 'yes'
        )
    )
  );

  $myquery = new WP_Query($args);
John Locke
John Locke
15,479 Points

Thank you for your help, Szabolcs. I'm typing this out on my phone, I did see the missing parentheses, I cannot scroll down to edit it. I have been trying to tackle this problem all day, that is not what was preventing the query from working.

Szabolcs Légrádi
Szabolcs Légrádi
9,797 Points

Ah, ok. I posted my answer the same time you commented. :)

The query looks fine, so could you please copy the code that would display these posts? I am thinking the problem will be there.

John Locke
John Locke
15,479 Points

Szabolcs:

The problem is not in the loop, it is in the query. This works:

  <?php
   wp_reset_postdata();
   $myargs = array (
       'showposts' => 6
   );
   $myquery = new WP_Query($myargs);
     if($myquery->have_posts() ) :
       while($myquery->have_posts() ) : $myquery->the_post();
   ?>
   <p>
     <?php the_title(); ?>
   </p>
   <?php endwhile;
         endif;
         wp_reset_postdata();
    ?>

But if I add the meta_query array to the $args, it fails to show anything on the page.

If someone knows the proper way to vett specific posts from the loop, in the sidebar, using either a checkbox or true/false custom field in the query, using Advanced Custom Fields, that is what I need to know.

Matt Campbell
Matt Campbell
9,767 Points

I've been deep in meta queries today as well. I was doing something similar to you and it didn't work. I use this, taken from my code so will need tweaking, and now I can loop meta data.

        $sets = get_post_meta($post->ID, 'sets', true);

        foreach($sets as $sets){
        echo $sets['number-of-sets'];
        };

Might help????

John Locke
John Locke
15,479 Points

I'll try it and let you know, Matthew. Thanks for the assist.

John Locke
John Locke
15,479 Points

This ended up being a solution that worked. I used the Radio button selector in Advanced Custom Fields and set Required to "no".

  <?php
  wp_reset_postdata();
  $myargs = array (
      'showposts' => 6,
      'post_type' => 'post',
      'meta_key' => 'favorites',
      'meta_value' => 'yes'
  );
  $myquery = new WP_Query($myargs);
  if($myquery->have_posts() ) :
      while($myquery->have_posts() ) : $myquery->the_post();
      the_title(); 

      endwhile;
      endif;
      wp_reset_postdata();
      ?>

Thanks for posting the solution. This was very helpful.

Neli B. S. Santana
Neli B. S. Santana
383 Points

Thanks for that! I had the exact same problem, now it's solved :)