Wordpress:尝试按标签获取帖子

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12272997/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 19:00:03  来源:igfitidea点击:

Wordpress: trying to get posts by tag

wordpresstags

提问by user1647208

I've written some code which automatically creates some posts and adds a tag to them. I can see the tags in the 'All posts' admin panel and I can click on the posts 'Tag' link to get just those posts with the tags.

我编写了一些代码,可以自动创建一些帖子并为它们添加标签。我可以在“所有帖子”管理面板中看到标签,我可以单击帖子“标签”链接以获取带有标签的帖子。

However, in a plugin that I'm writing using $wp_query no matter what parameters I pass in, I just get the complete list of posts back whether they have the tag that I am looking for or not.

但是,在我使用 $wp_query 编写的插件中,无论我传入什么参数,我都会得到完整的帖子列表,无论它们是否有我正在寻找的标签。

Here's my code:

这是我的代码:

// Now retrieve all items matching this brand name . . .
$query=new WP_Query(array('posts_per_page=5', array('tag' => array($brand_name))));

// The Loop
while ( $query->have_posts() ) : $query->the_post();
    echo '<li>';
    the_title();
    echo '</li>';
endwhile;

// Reset Post Data
wp_reset_postdata();

This produces 10 results when I've told it only to return 5. In reality I should only get 2 posts back as that's the total number with the tag.

当我告诉它只返回 5 个时,这会产生 10 个结果。实际上,我应该只返回 2 个帖子,因为这是带有标签的总数。

Looking around on the web there seems to be a lot of people having the same problem but no solutions. I must have tried about 10 different ways of specifying the tag but the fact that the number of posts returned is wrong suggests I've either got something completely wrong or there is some kind of bug. Wordpress version is 3.4.1 if it helps.

在网上环顾四周,似乎有很多人遇到了同样的问题,但没有解决方案。我必须尝试过大约 10 种不同的方式来指定标签,但返回的帖子数量错误这一事实表明我要么完全错误,要么存在某种错误。如果有帮助,Wordpress 版本是 3.4.1。

Can any Wordpress pro's shed light on this ?

任何 Wordpress 专业人士都可以对此有所了解吗?

Thanks in advance !

提前致谢 !

回答by BeRocket

Answer was found here - https://codex.wordpress.org/Template_Tags/get_posts

答案在这里找到 - https://codex.wordpress.org/Template_Tags/get_posts

Following example displays posts tagged with 'jazz', under 'genre' custom taxonomy, using 'tax_query'

以下示例使用“tax_query”在“流派”自定义分类法下显示标记为“爵士”的帖子

$args = array(
    'tax_query' => array(
        array(
            'taxonomy' => 'genre',
            'field' => 'slug',
            'terms' => 'jazz'
        )
    )
);
$postslist = get_posts( $args );

So for you it will be

所以对你来说

$args = array( 
        'posts_per_page' => 5,
        'tax_query'      => array(
            array(
                'taxonomy'  => 'post_tag',
                'field'     => 'slug',
                'terms'     => sanitize_title( $brand_name )
            )
        )
    );

$postslist = get_posts( $args );

回答by The Alpha

Try this

尝试这个

$original_query = $wp_query;
$wp_query = null;
$args=array('posts_per_page'=>5, 'tag' => $brand_name);
$wp_query = new WP_Query( $args );
if ( have_posts() ) :
    while (have_posts()) : the_post();
        echo '<li>';
        the_title();
        echo '</li>';
    endwhile;
endif;
$wp_query = null;
$wp_query = $original_query;
wp_reset_postdata();

回答by Little Systems

In your code, try:

在您的代码中,尝试:

$query=new WP_Query(array('posts_per_page=5', 'tag' => $brand_name));

instead of:

代替:

$query=new WP_Query(array('posts_per_page=5', array('tag' => array($brand_name))));

For further details, see https://codex.wordpress.org/Class_Reference/WP_Query#Tag_Parameters(and as mentioned on a recent duplicate post).

有关更多详细信息,请参阅https://codex.wordpress.org/Class_Reference/WP_Query#Tag_Parameters(以及最近的重复帖子中提到的)。

Note:$brand_name could be an array of strings, or comma separated values, etc., and the above code should work.

注意:$brand_name 可以是字符串数组,也可以是逗号分隔值等,上面的代码应该可以工作。

Alternatively, try:

或者,尝试:

$myPosts = get_posts(array('tag' => $brand_name));

See https://codex.wordpress.org/Template_Tags/get_posts

https://codex.wordpress.org/Template_Tags/get_posts