$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>meta query - WP_Query with meta_query no results|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

meta query - WP_Query with meta_query no results

matteradmin9PV0评论

I have a custom post type and can get them using WP_Query with the following array:

$args = array(  
       'post_type' => 'projects',
       'post_status' => 'publish',
       'posts_per_page' => 10,
   );

This works fine but now I have added a new field to the post type called 'featured_project' which is a checkbox with the value of 1, so to get only results with the checkbox checked I have added a meta_query, but with this meta_query I get no results and can't figure out why:

$args = array(  
       'post_type' => 'projects',
       'post_status' => 'publish',
       'posts_per_page' => 10,
       'meta_query' => array(
        'relation' => 'AND',
            array(
                'key'     => 'featured_project',
                'value'   => 1,
                'compare' => '=',
            ),
        )
   );

I don't know why, I have googled this time and again and my code looks correct I think but with the meta_query addition I just get an empty set, could anyone please advise? I'm using WordPress v5, php 7.1

I have just tried a new array format and changed the value of the 'featured_project' checkbox to 'featured', and still got no results.

   $args = array(  
       'post_type' => 'projects',
       'post_status' => 'publish',
       'posts_per_page' => 10,
       'meta_query' => array(
            array(
                'key'     => 'featured_project',
                'value'   => 'featured',
                'compare' => '=',
            )
        )
   );

And tried:

$args = array(  
       'post_type' => 'projects',
       'post_status' => 'publish',
       'posts_per_page' => 10,
       'meta_query' => array(
        'relation' => 'AND',
            array(
                'key'     => 'featured_project',
                'value'   => 'featured',
                'compare' => '=',
            ),
        )
   );

I have a custom post type and can get them using WP_Query with the following array:

$args = array(  
       'post_type' => 'projects',
       'post_status' => 'publish',
       'posts_per_page' => 10,
   );

This works fine but now I have added a new field to the post type called 'featured_project' which is a checkbox with the value of 1, so to get only results with the checkbox checked I have added a meta_query, but with this meta_query I get no results and can't figure out why:

$args = array(  
       'post_type' => 'projects',
       'post_status' => 'publish',
       'posts_per_page' => 10,
       'meta_query' => array(
        'relation' => 'AND',
            array(
                'key'     => 'featured_project',
                'value'   => 1,
                'compare' => '=',
            ),
        )
   );

I don't know why, I have googled this time and again and my code looks correct I think but with the meta_query addition I just get an empty set, could anyone please advise? I'm using WordPress v5, php 7.1

I have just tried a new array format and changed the value of the 'featured_project' checkbox to 'featured', and still got no results.

   $args = array(  
       'post_type' => 'projects',
       'post_status' => 'publish',
       'posts_per_page' => 10,
       'meta_query' => array(
            array(
                'key'     => 'featured_project',
                'value'   => 'featured',
                'compare' => '=',
            )
        )
   );

And tried:

$args = array(  
       'post_type' => 'projects',
       'post_status' => 'publish',
       'posts_per_page' => 10,
       'meta_query' => array(
        'relation' => 'AND',
            array(
                'key'     => 'featured_project',
                'value'   => 'featured',
                'compare' => '=',
            ),
        )
   );
Share Improve this question edited Dec 14, 2018 at 23:46 Dave Romsey 17.9k11 gold badges56 silver badges70 bronze badges asked Dec 14, 2018 at 21:57 Barry PooreBarry Poore 213 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

Your code looks correct. Have you checked your stored meta data to confirm the value stored is 1? Also, have you tried comparing to a string value, eg '1' instead of 1? Just a stab in the dark, but worth a shot.

Assuming your stored values are correct, and seeing as the values can only ever be 1, you could try using:-

'compare' => 'LIKE'

LIKE usually works as a partial match, but in your case, it shouldn't make any difference.

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far