2016-08-23 64 views
1

我有一個自定義帖子類型圖片與自定義分類稱爲image_tag(它是像分類像分類)。這裏有可能被使用的標記的一些例子:Wordpress tax_query「and」operator not as functional expected

Structure (id: 25) 
- House (id: 56) 
- Skyscraper 
Nature 
- Animal 
- Plant (id: 41) 

所以,我想通過與「和」運營商一起選擇多個標籤通過圖像向下鑽取。例如,查找所有照片工廠 s和house s。

$query_args = array(
    'post_type' => 'image', 
    'tax_query' => array(
    array(
     'taxonomy' => 'image_tag', 
     'terms' => array(25, 41), // IDs of "structure" and "plant" 
     'operator' => 'and', 
    ), 
), 
); 

然後我沒有得到任何結果:

$query_args = array(
    'post_type' => 'image', 
    'tax_query' => array(
    array(
     'taxonomy' => 'image_tag', 
     'terms' => array(41, 56), // IDs of "plant" and "house" 
     'operator' => 'and', 
    ), 
), 
); 

這工作正常,當我嘗試包括父詞條,例如問題開始。我猜是因爲我使用「和」運算符,Wordpress不包括「結構」術語的子代。有沒有人有一個想法,我怎麼能得到這個工作?

+0

兩個簡單的問題: 1)只是爲了確定。你能100%確認是否有應該退回的帖子? 2)你是否明確設置'include_children'爲'true'?我相信那裏至少有一個需要你這樣做的錯誤(儘管它通常是默認的)。 – mrwweb

+0

好的:1)是的,有一些帖子標有這兩個詞(或其中的孩子),2)是的,我也嘗試過'include_children',可悲的是,似乎沒有任何影響。 – dkeeling

+0

很高興知道。我的下一個猜測是@ hubert-popp的答案如下。我會嘗試。 – mrwweb

回答

1

因此,它應該是這樣的:

'relation' => 'AND', 
    array(
     'taxonomy' => 'image_tag', 
      'field' => 'term_id', 
      'terms' => array(25), 
     ), 
     array(
     'taxonomy' => 'image_tag', 
      'field' => 'term_id', 
      'terms' => array(41), 
     ), 
     ), 
0

更新:你忘了按「關閉「項」和「運營商」這樣的

$query_args = array(
    'post_type' => 'image', 
    'tax_query' => array(
    array(
     'taxonomy' => 'image_tag', 
     'field' => 'term_id', 
     'terms' => array(25, 41), // IDs of "structure" and "plant" 
     'operator' => 'in' 
    ), 
), 
); 
+0

感謝您的回覆!不幸的是,這對我來說似乎沒有什麼不同。 (我認爲'term_id'是該'field'設置的默認值。) – dkeeling

+0

但使用我的代碼cuz你的代碼中有一些錯誤,比如'條款你必須像這樣關閉它''也'運營商' –

+0

謝謝,我修復了這些錯別字。這只是我使用的代碼的一個示例,而不是完整的示例,所以我仍然得到相同的結果。 – dkeeling

0
$query_args = array(
'post_type' => 'image', 
'tax_query' => array(
    'relation' => 'AND', 
     array(
     'taxonomy' => 'image_tag', 
     'field' => 'term_id', 
     'terms' => array(25, 41), // IDs of "structure" and "plant" 
     'operator' => 'in' 
    ), 
    ), 
); 

我有一個類似的問題,試試這個!

+0

感謝您的回答!這是有效的,但不幸的是,我不在尋找的方式。此解決方案返回**結構**中的圖像和**植物**中的圖像(這擴大了搜索範圍)。我需要的是通過返回具有**結構**和**植物**標籤(或這些標籤的子項)的圖像來縮小搜索範圍。 ......我不知道我描述它的方式是否合理,哈哈 – dkeeling

相關問題