Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm having a lot of trouble using php query_posts(array('category__and'=>array(1,3))) - it seems to be pulling anything that's in category 1 regardless of whether it's also in category 3 and I only want the posts if they exist in both categories. And ultimately, I need a bunch of OR statements anyway, so is there a better way than query_posts to do this?

I want a page of posts that exist in a variety of multiple categories -- i.e:

categories 1 AND 3 OR categories 1 AND 5 OR categories 1 AND 6

But not categories 1 AND 4

Any help would be greatly appreciated. Thanks.

share|improve this question

3 Answers 3

up vote 1 down vote accepted

Strange, that usage of category__and should be working.

As for following logic I don't think you can accomplish anything that complex with query parameters. There are several possible ways to deal with it that I can think of:

  • simplify category structure and/or selection logic;
  • modify raw SQL with posts_where filter;
  • store selection criteria in post meta.
share|improve this answer
    
This actually was the best advice for me. I was trying to do too many things with this category select and by simplifying it, the category_and worked just fine. –  user3997 Mar 17 '11 at 18:20

You might be able to achieve something like what you're looking for with the tax_query argument.

In your case (untested, may yet fail), something like this will serve to get posts (in category 1) AND (in either category 3, 5, or 6) AND (NOT in category 4):

$tax_query = array(
        'relation' => 'AND',
    array(
        'taxonomy' => 'category',
        'terms' => array( 1 ),
    ),
    array(
        'taxonomy' => 'category',
        'terms' => array( 3, 5, 6 ),
    ),
    array(
        'taxonomy' => 'category',
        'terms' => array( 4 ),
        'operator' => "NOT IN"
    ),
);

query_posts( array( 'tax_query' => $tax_query ) );
share|improve this answer

You should be able to do it using the new tax_query arguments, like so.

query_posts( array(
    'tax_query' => array(
        'relation' => 'OR',
        array(
            'taxonomy' => 'category',
            'field' => 'slug',
            'terms' => array('term1', 'term3'),
            'operator' => 'AND'
        ),
        array(
            'taxonomy' => 'category',
            'field' => 'slug',
            'terms' => array('term1', 'term5'),
            'operator' => 'AND'
        ),
        array(
            'taxonomy' => 'category',
            'field' => 'slug',
            'terms' => array('term1', 'term6'),
            'operator' => 'AND'
        )
    )
) );

All category/tag parameters are just wrappers for tax_query's to now, so when you use category__in or any of the others, you're essentially just taking a shortcut to writing out a tax_query.

share|improve this answer
    
Fancy. I so need to read up on these... :) –  Rarst Mar 16 '11 at 23:51
    
Thanks so much for this, but this locks it down so tight I get no posts at all. Does it matter if these are custom post types as well? I did try adding my additional criteria (post type, metas, sorting) but it didn't make any difference. I'm kin dof thinking I need to re-think my category structure on this. –  user3997 Mar 17 '11 at 14:27
    
@maddy - Could you add the code you're currently using to the question please. –  t31os Mar 18 '11 at 13:07

Your Answer

 
discard

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