admin管理员组文章数量:1418088
i have "Tag1" and "Tag2" i want to find most recent post with both tags.So far i have tried this, i know im close, but it is listing all posts containing either tag.
$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;
}
i have "Tag1" and "Tag2" i want to find most recent post with both tags.So far i have tried this, i know im close, but it is listing all posts containing either tag.
$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;
}
Share
Improve this question
asked Aug 25, 2019 at 6:50
OnelostpuppyOnelostpuppy
32 bronze badges
1
- $query = new WP_Query( array( 'tag' => 'agirt+fetch' ) ); ended up working for me as well, just wanted to post this here as an alternate – Onelostpuppy Commented Aug 26, 2019 at 8:36
1 Answer
Reset to default 0Using the tax_query
param in your args (and replacing the dot with a comma) should get you what you're looking for:
$args = array(
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'post_tag',
'field' => 'slug',
'terms' => 'fetch',
),
array(
'taxonomy' => 'post_tag',
'field' => 'slug',
'terms' => 'agirt',
)
)
);
本文标签: phpSearch for single post by 2 tags
版权声明:本文标题:php - Search for single post by 2 tags 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745217742a2648249.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论