Wordpress meta_query compare not in issue

2019-08-05 06:53发布

问题:

I have a issue with wordpress meta query.

I make a checkbox in meta box, When user check the check box the post is become featured. Now I call all posts except featured, but I don't get it.

Here is my code:

$wa_story_args = array(
                'post_type' => 'stories',
                'posts_per_page' => 999,
                'orderby' => 'DESC',
                'meta_query' => array(
                    array(
                    'meta_key' => 'wa_featured_story',
                    'meta_value' => 'featured',
                    'compare' => 'NOT IN'
                        )
                )
            );

回答1:

Just replace "meta_value" by "value" and "meta_key" by "key":

$wa_story_args = array(
            // ...
            'meta_query' => array(
                array(
                'key' => 'wa_featured_story',
                'value' => 'featured',
                'compare' => 'NOT IN'
                    )
            )
        );


回答2:

Try using the '!=' operator instead of 'NOT IN' as this is usually used to check against arrays.

You should also consider adding a OR 'NOT EXISTS' condition.