我有“Tag1”和“Tag2”,我想找到包含这两个标签的最新帖子。到目前为止,我已经尝试了这个,我知道我很接近了,但它列出了所有包含任何一个标签的帖子。
$args = array(
\'relation\' => \'AND\',
array(
\'taxonomy\' => \'post_tag\',
\'terms\' => \'agirt\',
\'field\' => \'slug\',
\'operator\' => \'AND\'.
),
array(
\'taxonomy\' => \'post_tag\',
\'terms\' => \'fetch\',
\'field\' => \'slug\',
),
);
$the_query = new WP_Query($args);
if ( $the_query->have_posts() ) {
$html = \'<ul>\';
while ( $the_query->have_posts() ) {
$the_query->the_post();
$post_title = $the_query->post->post_title;
echo "<li>$post_title</li>";
}
$html .= \'</ul>\';
echo $html;
}
最合适的回答,由SO网友:Steven 整理而成
使用tax_query
参数中的param(并用逗号替换点)应该可以得到您想要的:
$args = array(
\'tax_query\' => array(
\'relation\' => \'AND\',
array(
\'taxonomy\' => \'post_tag\',
\'field\' => \'slug\',
\'terms\' => \'fetch\',
),
array(
\'taxonomy\' => \'post_tag\',
\'field\' => \'slug\',
\'terms\' => \'agirt\',
)
)
);