如何按类别和标签查询帖子?

时间:2010-11-17 作者:hannit cohen

我正在尝试显示与类别X和标记Y相关的帖子列表。我尝试了以下代码:

$args = array(
    \'posts_per_page\' => 4,
    \'tag_id\' => $tag_id,
    \'cat\' => $cat_id,
);
query_posts($args);
但它不能正常工作,并返回co目录中的所有帖子。

很想听听你的见解

5 个回复
最合适的回答,由SO网友:Chris_O 整理而成

编辑:有关查询类别和标记交点的正确方法,请参见下文

global $wp_query;
        $args = array(
        \'category__and\' => \'category\', //must use category id for this field
        \'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;

SO网友:James Piggot

我认为这是WordPress中其他地方评论过的错误,请尝试使用标记的名称而不是ID,这样应该可以:

$args = array(
    \'posts_per_page\' => 3,
    \'tag\' => \'review\',
    \'cat\' => 9,
);
query_posts($args);
让我们知道您的进展,不知道名称中有多个单词的标签会发生什么。

SO网友:Wouwei

我偶然发现了同样的问题,并通过请求MySQL解决了它。

简而言之:get\\u post($args)将返回类别为MyCategory的帖子OR 标记=MyTag。

你想要的是改变你的ORAND .

我的逻辑是直接使用MySQL查询:

查询1=选择所有具有类别MyCat的帖子查询2=选择所有具有标记MyTag的帖子最后:选择查询1和查询2中的所有帖子我使用wpdb 而不是query\\u post();

A bit of code (返回类别为MyCat和标记为MyTag的已发布帖子)

    $query_byTag="
            SELECT wp_posts.ID
            FROM wp_posts, wp_term_relationships, wp_terms
            WHERE wp_posts.ID = wp_term_relationships.object_id
            AND wp_terms.term_id = wp_term_relationships.term_taxonomy_id
            AND wp_terms.name = \'MyTag\'";

    $query_byCat="
            SELECT wp_posts.ID
            FROM wp_posts, wp_term_relationships, wp_terms
            WHERE wp_posts.ID = wp_term_relationships.object_id
            AND wp_terms.term_id = wp_term_relationships.term_taxonomy_id
            AND wp_terms.name = \'MyCat\'";

$query ="
            SELECT      wp_posts.post_title AS title , 
                        wp_posts.post_content AS content,
                        wp_posts.post_date AS blogdate 
            FROM wp_posts
            WHERE wp_posts.post_status = \'publish\'
            AND wp_posts.ID IN (".$query_byTag.")
            AND wp_posts.ID IN (".$query_byCat.")
            ORDER BY wp_posts.post_date DESC ";

$result= $wpdb->get_results($query);
这是一种肮脏的做法,但我希望它能有所帮助=)

SO网友:Oleksandr Chopenko

This code works:

$args = array(
    \'tag\' => get_queried_object()->slug, // If permalink like example.com/tag/example-tag, etc.
    \'posts_per_page\' => -1,
    \'tax_query\' => array( 
        array(
            \'taxonomy\' => \'category\', // Taxonomy, in my case I need default post categories
            \'field\'    => \'slug\',
            \'terms\'    => \'interior\', // Your category slug (I have a category \'interior\')
        ),
        ) 
); // Get all posts
$posts_new = get_posts( $args );
SO网友:mal
SELECT wp_posts.post_name
FROM wp_posts, wp_term_relationships, wp_terms, wp_term_taxonomy
WHERE wp_posts.ID = wp_term_relationships.object_id
AND wp_terms.term_id = wp_term_taxonomy.term_id
AND wp_term_taxonomy.term_taxonomy_id = wp_term_relationships.term_taxonomy_id
AND wp_terms.name = "MY TAG"
结束

相关推荐

Tags page not for post

符合Wordpress: category page not for post\'s.我使用wordpress为flash游戏创建网站,所以我没有特定的贴子页面。我通过新贴子添加每个游戏。php?post\\u type=游戏,你可以看到这不是wordpress的常规帖子。现在我尝试对标签进行相同的处理,但再次卡住。我现在拥有的:<?php if (is_page() ) { $tag = get_post_meta($posts[0]->ID, \'tag\', true);