WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have two categories that will always stay the same. They are called "Events" and "Show on frontpage". The latter is a child category to Events.

What i want to do is, when fetching posts (using WP_Query), check if the post is in the Events category. If it is, and it is NOT in the "Show on frontpage" category, don't fetch the post at all.


- post is in Events but NOT in Show on frontpage:     Don't fetch post
- post is in Events AND in Show on frontpage:         Fetch post
- post is NOT in Events:                              Fetch post

The goal is to achieve the functionality only using WP_Query. Maybe it's possible using some special category__in or category__not_in queries or something. The trick is to kind of have an if statement inside the query, because there are three possible scenarios.


share|improve this question
An if statement in your loop would prob be the easiest. I think the last scenario is what really throws it off. If you didn't have the third scenario you could use the category__and argument.If you switched your show on front page setting to meta data instead of a taxonomy you could use something like $args = array('meta_query' => array( array('key'=>'show_on_front','value'=>'your_active_value','compare'=>'='))); – Cole Feb 18 '13 at 14:34
I'm using an if statement right now, but that doesn't work quite as i want it to. If the first scenario is met (don't fetch), i just skip the post using continue;. The problem is, if i have set wordpress to display 10 posts per page, and it skips some posts, then i will end up with less than 10 posts whenever the don't fetch scenario is met. This is a bigger issue than it seems, because i'm loading more posts using ajax, on some pages it only fetches one post per "load", so if that post is skipped, the script will assume there are no more posts, and return nothing. – qwerty Feb 18 '13 at 14:59
up vote 0 down vote accepted

Here we go, try a tax_query instead http://codex.wordpress.org/Class_Reference/WP_Query#Taxonomy_Parameters

'tax_query' => array(
    'relation' => 'OR',
        'taxonomy' => 'category',
        'field' => 'slug',
        'terms' => array( 'event','show_on_front' ),
        'operator' => 'AND',
        'taxonomy' => 'category',
        'field' => 'slug',
        'terms' => array( 'event' ),
        'operator' => 'NOT IN'
share|improve this answer
Sweet man! I'll give that a shot, thank you! – qwerty Feb 18 '13 at 15:45
Hmm, code seems to be perfectly valid, but i'm not getting the correct results. It's as if it just ignores the query entirely. Have a look at my entire $config that i send into WP_Query: pastebin.com/KLFj5JBS Do you see anything that might cause it to fail? Slugs are correct. I'll take a deeper look at it tomorrow, time to go home now. – qwerty Feb 18 '13 at 16:06
I think my last edit should do the trick – Cole Feb 18 '13 at 16:52
I think the original 'relation' => 'OR' was correct. – user27457 Feb 18 '13 at 17:22
Still not entirely sure what caused it to not work in the beginning, might have been the fact that i had show_on_front as a sub-category to event. It works fine now, thank you! – qwerty Feb 19 '13 at 7:39

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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