Wordpress 按标签和类别查询帖子

Wordpress query posts by tag and category

我正在尝试创建一组 WP 页面,其中包含所有具有标签 X 和类别 Y 的帖子的索引。

我发现 this 似乎完全符合我的要求,我知道如何为每个索引页编辑它,但我不知道 where/how 使用实际代码来创建索引页。

global $wp_query;
    $args = array(
    'category__and' => 'category', 
    'tag__in' => 'post_tag', //must use tag id for this field
    'posts_per_page' => -1); //get all posts

$posts = get_posts($args);
    foreach ($posts as $post) :
//do stuff 
 endforeach;

TIA 为您提供帮助。

您使用该代码替换模板循环中的基本查询。

https://codex.wordpress.org/Function_Reference/query_posts

这终于奏效了 - 请注意,我需要类别 ID 而不是标签 slug。

<?php if ( have_posts() ) : ?>

<?php query_posts( 'cat=6&tag=a1&&orderby=title&order=asc' );?>

<?php while ( have_posts() ) : the_post();?>

<?php get_template_part( 'content', 'search' ); ?>

<?php endwhile; ?>

<?php else : ?>