post 循环中的多个标签 ID

Multiple tag id in post loop

如何在 WP_Query() 中添加多个标签。所以,我有三个标签:

  1. 名字(标签 id="10")
  2. 中间名(标签 id="20")
  3. 姓氏(标签 id="30")

    $args = array( 
     'post_type' => 'post',
        'paged'=>$paged,
        'posts_per_page' => 15,             
        'orderby' => 'date',
        **'tag_id' =>  array(10,20,30),**           
        'order' => 'DESC'
        );
    

当然上面不行。

如何显示来自多个标签的帖子?

谢谢!

有一个 tag__in 参数专门用于此:

$args = array( 
    'post_type' => 'post',
    'paged'=> $paged,
    'posts_per_page' => 15,             
    'orderby' => 'date',
    'tag__in' =>  array(10,20,30),
);

如果您使用的是WP_Query,那么您可以按照下面的方式进行操作。

//Display posts that are tagged with both tag id 37 and tag id 47:
$query = new WP_Query( array( 'tag__and' => array( 37, 47 ) ) );

//To display posts from either tag id 37 or 47, you could use tag as mentioned above, or explicitly specify by using tag__in:
$query = new WP_Query( array( 'tag__in' => array( 37, 47 ) ) );

更多可以访问官方linkhttps://codex.wordpress.org/Class_Reference/WP_Query