0

I am passing a variable $category to category__and, and it works unless $category = 0;

'category__and' => array(4) // works
'category__and' => array($category, 4) // works if category > 1
'category__and' => array(0, 4) // does not work.

I originally tried to just use cat, but if the post has cat 4 and NOT cat 6, it will still show it. This is why I went to category__and.

'cat' => '4,6' // will show a post with category 4 even if 6 isn't present

I could write a conditional to check the value of $category but that seems unnecessary and I think I'm overlooking something.

5
  • under what conditions would you end up with a category ID of 0? IDs start at 1, 0 can never be a valid category ID.
    – Milo
    Feb 4 '15 at 20:53
  • @Milo I wrote a post handler that has defaults set to 0. Would it better to have $category = '' as the default? Feb 4 '15 at 21:15
  • it would be better to just verify that it's a valid value before trying to do anything with it.
    – Milo
    Feb 4 '15 at 21:16
  • @Milo ah, I could create that array before running the query. that does work. good call :) Feb 4 '15 at 21:30
  • That is normal. You are saying "get me posts that are in category 0 and 4", so it works but you have not any post taht mathc the condition. Checking the value of $category before you use it on the query is the only way I can imagine.
    – cybmeta
    Feb 5 '15 at 9:25
1

You could use tax_query to solve this problem.

I assume 4 and 6 are only categories, so you'll probably need to construct the queries within the $tax_query array rather than declare it, but let us know if that is a problem. Just remember that relation => 'AND' must be included.

$tax_query = array(
    relation => 'AND',
    array(
        'taxonomy'          => 'category',
        'field'             => 'term_id',   // 'term_id' by default, so just here as an example
        'terms'             => $cat,
        'include_children'  => false,       // true by defualt
        'operator'          => 'IN'         // 'IN' by default, so just here as an example
    ),
    array(
        'taxonomy'          => 'category',
        'terms'             => $cat2,
        'include_children'  => false,       // true by defualt
    )
)

Once you've set $tax_query up as you require, just add it to the query, along with any other arguments you desire, like so -

'tax_query' => $tax_query

Check out the Class Reference for WP_Query for more details - http://codex.wordpress.org/Class_Reference/WP_Query#Taxonomy_Parameters


Update

The OP has suggested that $cat may actually contain multiple categories as opposed to just one, so the code below (untested) should allow for this while still using the `tax_query' parameter of the query.

/** Ensure that '$cat' is cast as an array */
if(!is_array($cat) && !empty($cat))
    $cat = explode(',', $cat);
    
/** Ensure that '$cat2' is cast as an array */
if(!is_array($cat2) && !empty($cat2))
    $cat2 = explode(',', $cat2);
    
/** Create a single array of all categories */  
$cats_array = $cat + $cat2

/** Create the '$tax_query' variable and cast it as an array */
$tax_query = array();

/** Add each category to the 'tax_query' */
if(!empty($cats_array)) : foreach($cats_array as $single_cat) :

        $tax_query[] = array(
            'taxonomy'          => 'category',
            'terms'             => $single_cat,
            'include_children'  => false,       // true by defualt
        )
        
    endforeach;
endif;

/** Check to see if there is more than one category within the 'tax_query' and if so set the relation to 'AND' */
if(count($tax_query) > 1)
    $tax_query['relation'] = 'AND';

The only thing to note here is that you should check if(!empty($tax_query)) before adding it to your query.

You can use this method for custom taxonomies as well.

5
  • I have two variables I'd have to pass into this - $cat and $cat2. I already wrote the conditional to check the $category value but if I get a chance I'll test this out, and definitely remember in the future. I've never thought about using tax query. thanks! Feb 5 '15 at 15:03
  • You're welcome. If it's only every $cat and $cat2 you can use the example as modified above. However if you have say an array of category ID's you can use a foreach loop to construct the tax_query.
    – David Gard
    Feb 5 '15 at 15:27
  • Yup, only ever 2. The problem though is $cat could technically be "1,2,3,5,6" so I would need to check it's properties and split into an array. This is sidetracking from the actual issue above though, which your solution does like solve. I feel bad that I may never test it because it currently works, but I know that when updates come I'll have to try this. Feb 5 '15 at 17:12
  • Ok, you can still use the method above if $cat contains a list of categories, as opposed to just one. I'll see if I get time today and update the answer (if not for you then for others in the future).
    – David Gard
    Feb 6 '15 at 9:50
  • I've now updated my answer to take in to account your comments.
    – David Gard
    Feb 6 '15 at 10:18

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.