WP_Query tax_query多个分类法和术语

问题描述:

我在使用多个分类法和术语返回帖子时遇到了一些麻烦。希望有比我更多知识的人能帮助我理解。WP_Query tax_query多个分类法和术语

我正在使用选择菜单来填充页面的分类信息(在本例中是产品页面)的选择选项。对于tax_query中的单个分类和术语来说,所有这一切都很好,但只要我尝试使用数组来传递多个数字,我就不会再返回任何东西。这似乎很简单,但我错过了一些东西。有任何想法吗?

这里是我的工作:

$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 
    ) 
) 
) 
+0

$ producttype是一个数组?和同样的$ businessunit? –

你的错误是一个关键的阵列tax_query =>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

+0

好,吉列尔莫!这就是我使用记事本得到的结果。但是,我修复了我的拼写错误,它现在正在返回结果,但它仍未返回正确的结果。看起来数组中的第二项被忽略。 – TRex

+0

不是。这是对的。你是对的。我需要使用AND运算符而不是OR。都好。谢谢! – TRex