Wordpress 通过标签名获取 post

Wordpress get post by tag name

我想按标签 name 而不是标签 slug

查询帖子

我试过了,但它只适用于标签 slug

$args = array( 'post_type' => "news_type", 'posts_per_page' => -1, 'tag' => "my tag" );

另一种方法是通过标签名称获取标签,然后从标签中获取 slug。

(我只能在我的代码中访问标签名称)

实现此目的的最佳方法是使用 tax_query。您可以将 term_id(默认设置)、nameslug 传递给 terms 参数

您可以尝试这样的操作:

$args = array(
    'post_type' => 'news_type',
     'posts_per_page' => -1,
    'tax_query' => array(
        array(
            'taxonomy' => 'post_tag',
            'field'    => 'name',
            'terms'    => 'NAME OF THE TAG',
        ),
    ),
);
$query = new WP_Query( $args );