問題描述

説我有以下分類術語:

Term 1
  Term 1.1
  Term 1.2
Term 2
  Term 2.1

如何獲得分配到術語 1 的職位,不包括分配到術語 1.1 或術語 1.2 的職位?

例如:

$pages = get_posts(array(
  'post_type' => 'page',
  'numberposts' => -1,
  'tax_query' => array(
    array(
      'taxonomy' => 'taxonomy-name',
      'field' => 'id',
      'terms' => 1 // Where term_id of Term 1 is "1".
    )
  )
);

還給了我分配了條款 1.1 和 1.2 的帖子。

謝謝。

最佳解決方案

在查看/wp-includes/taxonomy.php 中的 WP_Tax_Query 類時,我發現有一個默認為 true 的’include_children’ 選項。我修改了我原來的 get_posts() 調用與以下,它的工作原理:

$pages = get_posts(array(
  'post_type' => 'page',
  'numberposts' => -1,
  'tax_query' => array(
    array(
      'taxonomy' => 'taxonomy-name',
      'field' => 'id',
      'terms' => 1, // Where term_id of Term 1 is "1".
      'include_children' => false
    )
  )
));

更多查詢參數列表:http://codex.wordpress.org/Class_Reference/WP_Query#Taxonomy_Parameters

參考文獻

注:本文內容整合自 Google/Baidu/Bing 輔助翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:薇曉朵技術論壇。