最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

wp query - WP_Query - using category__and when one of the categories is 0

programmeradmin2浏览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.

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.

Share Improve this question edited Feb 4, 2015 at 20:39 Jacob Raccuia asked Feb 4, 2015 at 20:32 Jacob RaccuiaJacob Raccuia 4651 gold badge6 silver badges18 bronze badges 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 Commented Feb 4, 2015 at 20:53
  • @Milo I wrote a post handler that has defaults set to 0. Would it better to have $category = '' as the default? – Jacob Raccuia Commented Feb 4, 2015 at 21:15
  • it would be better to just verify that it's a valid value before trying to do anything with it. – Milo Commented Feb 4, 2015 at 21:16
  • @Milo ah, I could create that array before running the query. that does work. good call :) – Jacob Raccuia Commented Feb 4, 2015 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 Commented Feb 5, 2015 at 9:25
Add a comment  | 

1 Answer 1

Reset to default 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/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.

发布评论

评论列表(0)

  1. 暂无评论