0

I was reading this post to "Exclude posts with specific meta_value" but it does not work correctly. Here is the arguments I am passing to WP_Query :

      $args=array(
        'cat' => $catid,
        'category__in'=> $term_ids,
        'post_type' => 'post',
        'post_status' => 'publish',
        'posts_per_page' => -1,
        'meta_query' => array(
            array(
                'key' => 'featured_post',
                'value' => 'on',
            ),
        ),
      );

Using the above method, I can list posts which use meta key, but I do not want posts, with meta key "featured_post" to be displayed. If I change the value to something else then no post is displayed.

The meta value is simply a check box for featured posts.

  • I believe you are missing your compare argument for the meta_query. – jdm2112 Oct 30 '15 at 21:35
1

Use compare with NOT EXISTS to get posts without that meta key.

'meta_query' => array(
    array(
        'key'     => 'featured_post',
        'compare' => 'NOT EXISTS',
    ),
)
  • thanks, it works, stupid me I checked the meta key in the post and I thought otherwise. – Rain Man Oct 30 '15 at 21:39
  • a related question, if I have something like this where I am trying to get post with meta key, if I have multiple posts with meta key in same category, all of them are displayed. Is it possible to only display 1 post with meta key based on date? (considering we have many of them) – Rain Man Oct 30 '15 at 21:46
  • change posts_per_page to 1 instead of -1. -1 returns all. – Milo Oct 30 '15 at 21:50
  • yes, but I have several categories, if I change it to 1 then only 1 post from a category is displayed. I want the post with meta key from each category to be displayed. – Rain Man Oct 30 '15 at 21:57
  • so something like this where this is used for this section. So the recent post published under Grand children categories which has meta key. – Rain Man Oct 30 '15 at 22:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.