代码之家  ›  专栏  ›  技术社区  ›  TRex

WP\u Query tax\u查询多个分类和术语

  •  1
  • TRex  · 技术社区  · 7 年前

    我在返回包含多个分类法和术语的帖子时遇到了一些问题。希望比我有更多知识的人能帮助我理解。

    我使用选择菜单用页面的分类信息填充选择选项(在本例中为产品页面)。在tax_查询中使用单个分类法和术语都很好,但只要我尝试使用数组传递倍数,我就不再返回任何内容。这看起来很简单,但我遗漏了一些东西。有什么想法吗?

    以下是我的工作内容:

    $producttype = $_GET['ProductType'];
    $businessunit = $_GET['BusinessUnit'];
    $products = new WP_Query( array( 
      'post_type' => 'products',
      'posts_per_page' => 15,
      'orderby' => 'title',
      'order'   => 'ASC',
      'paged' => $paged,
      'tax_query' => array(
        'relation' => 'OR',
         array(
           'taxonomy' => 'producttype',
           'field' => 'name',
           'term' => $producttype
         ),
         array(
           'taxonomy' => 'businessunit',
           'field' => 'name',
           'term' => $businessunit
         )
      )
    )
    
    1 回复  |  直到 7 年前
        1
  •  2
  •   Guillermo Andres Fuentes Moral    7 年前

    您的错误是键数组tax\u查询=> term terms

    $producttype = $_GET['ProductType'];
    $businessunit = $_GET['BusinessUnit'];
    $products = new WP_Query( array( 
      'post_type' => 'products',
      'posts_per_page' => 15,
      'orderby' => 'title',
      'order'   => 'ASC',
      'paged' => $paged,
      'tax_query' => array(
        'relation' => 'OR',
         array(
           'taxonomy' => 'producttype',
           'field' => 'name',
           'terms' => $producttype
         ),
         array(
           'taxonomy' => 'businessunit',
           'field' => 'name',
           'terms' => $businessunit
         )
      )
    

    wordpress