Support

Account

Home Forums ACF PRO WP_Query filtered by custom field using meta_key and meta_value

Helping

WP_Query filtered by custom field using meta_key and meta_value

  • Hello,

    I’ve followed the tutorial on this and looked at another forum thread and I still can’t figure out what I’m doing wrong. I suspect it’s simple, but can’t seem to see it.

    I’ve made a CPT called ‘articles’ and a custom field called ‘popular_or_scientific’ which allows an article to be classified as ‘Popular’ or ‘Scientific’. My decision to use ACF for this instead of WP categories or ‘tags’ is purely UI/UX driven.

    In Custom Fields I set up a Checkbox:
    the_field('popular_or_scientific');

    with choices: Popular and Scientific.

    Here’s my code:

    <?php
    				$args = array(
    				  'post_type' => 'articles',
    				  'meta_key'		=> 'popular_or_scientific',
    			  	  'meta_value'	=> 'Popular'
    
    				);
    
    				//Query
    				$the_query = new WP_Query( $args );
    				if( $the_query->have_posts() ) {
    				  while( $the_query->have_posts() ) {
    				    $the_query->the_post();
    				    the_title();
    				   
     				}
              }
              else {
                echo 'No articles found.';
              }
    			 ?>

    It returns nothing. When I tested with a simple text field, it works fine, so I suspect that I’m not doing something correctly with the Checkbox object.

    Any help would be appreciated.

    Thanks,
    Seth

  • Is your checkbox field set to return values?

Viewing 2 posts - 1 through 2 (of 2 total)

The topic ‘WP_Query filtered by custom field using meta_key and meta_value’ is closed to new replies.