0

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.

Scenarios:

- 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.

Ideas?

2
  • 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, 2013 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, 2013 at 14:59

1 Answer 1

0

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

...
'tax_query' => array(
    'relation' => 'OR',
    array(
        'taxonomy' => 'category',
        'field' => 'slug',
        'terms' => array( 'event','show_on_front' ),
        'operator' => 'AND',
    ),
    array(
        'taxonomy' => 'category',
        'field' => 'slug',
        'terms' => array( 'event' ),
        'operator' => 'NOT IN'
   )
)
5
  • 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, 2013 at 16:06
  • I think my last edit should do the trick
    – Cole
    Feb 18, 2013 at 16:52
  • 1
    I think the original 'relation' => 'OR' was correct.
    – user27457
    Feb 18, 2013 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, 2013 at 7:39
  • Tax query can also take an "include_children" boolean argument. If you wanted to keep it as a sub category.
    – Cole
    Feb 19, 2013 at 21:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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