Wordpress:WP_Query 中的多个分类术语
Wordpress: Multiple taxonomy terms in WP_Query
我正在尝试在 WP_Query 中获取多个分类术语。这是我的代码:
$category = get_field('portfolio_category'); //array of IDs like 14, 15, 16
<?php
$the_query = new WP_Query(array(
'post_type' => 'projects',
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => 'projectCategories',
'field' => 'term_id',
'terms' => array( implode(', ', $category ) ),
'operator' => 'AND'
)
),
));
?>
问题出在当前代码中我只能查询第一个词。
例如,我只能从 cat ID=14 获取项目。
我在这里做错了什么?如何查询来自多个术语的帖子?
谢谢。
因为 $category
已经是一个数组...将您的 terms
值更改为 $category
。所以您的完整查询将是:
$the_query = new WP_Query(array(
'post_type' => 'projects',
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => 'projectCategories',
'field' => 'term_id',
'terms' => $category,
'operator' => 'AND'
)
),
));
我正在尝试在 WP_Query 中获取多个分类术语。这是我的代码:
$category = get_field('portfolio_category'); //array of IDs like 14, 15, 16
<?php
$the_query = new WP_Query(array(
'post_type' => 'projects',
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => 'projectCategories',
'field' => 'term_id',
'terms' => array( implode(', ', $category ) ),
'operator' => 'AND'
)
),
));
?>
问题出在当前代码中我只能查询第一个词。 例如,我只能从 cat ID=14 获取项目。 我在这里做错了什么?如何查询来自多个术语的帖子?
谢谢。
因为 $category
已经是一个数组...将您的 terms
值更改为 $category
。所以您的完整查询将是:
$the_query = new WP_Query(array(
'post_type' => 'projects',
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => 'projectCategories',
'field' => 'term_id',
'terms' => $category,
'operator' => 'AND'
)
),
));