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.
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.
1 Answer
Reset to default 1You 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/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.
$category = ''
as the default? – Jacob Raccuia Commented Feb 4, 2015 at 21:15$category
before you use it on the query is the only way I can imagine. – cybmeta Commented Feb 5, 2015 at 9:25