如何按类别和标签查询帖子?
-
-
我认为使用query_posts()只能使用类别或标签.我不确定,但是功能的使用可能仅限于此功能,这意味着此功能可以正常工作,但它并没有执行您想要执行的操作.I think with query_posts() you can only make use of category or tag. I'm not sure, but maybe the use of the function is limited to that which would mean that this is correctly working but it doesn't do what you want to do it.
- 0
- 2010-11-17
- hakre
-
5 个回答
- 投票数
-
- 2010-12-03
编辑:有关查询类别和标签交集的正确方法,请参见下文.
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;
Edit: See below for proper way to query category and tag intersections.
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;
-
- 2010-12-04
我认为这是WordPress的一个错误,该错误已在其他地方进行了评论,请尝试使用标签的名称而不是ID,然后它应该起作用:
$args = array( 'posts_per_page' => 3, 'tag' => 'review', 'cat' => 9, ); query_posts($args);
让我们知道您的生活如何,不知道名称中包含多个单词的标签会发生什么情况.
I think this is bug in WordPress that has been commented on elsewhere, try using the name of the tag rather than the ID then it should work:
$args = array( 'posts_per_page' => 3, 'tag' => 'review', 'cat' => 9, ); query_posts($args);
Let us know how you get on, not sure what happens with tags with multiple words in the name.
-
- 2014-06-08
我偶然发现了同一问题,并通过发出MySQL请求解决了该问题.
简而言之: get_post($ args)将返回类别为=MyCategory OR tag=MyTag的帖子.您想要的是将 OR 更改为 AND .
我的逻辑是直接使用MySQL查询:
- 查询1=选择所有类别为MyCat的帖子
- 查询2=选择所有标签为MyTag的帖子
- FinalLY:选择查询1和查询2中的所有帖子.
我使用了 wpdb 而不是query_post();
一些代码 (返回类别为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);
这是一种肮脏的方式,但是我希望它能帮助您=)
I stumbled into this same issue and resolved it by making a MySQL request .
in short : get_post($args) will return you posts who have the category=MyCategory OR the tag=MyTag.
what you want is to change your OR to AND .
my logic was to go straight with a MySQL Query:
- Query 1 = Select all the posts who has the category MyCat
- Query 2 = Select all the posts who has the tag MyTag
- FinalLY : Select all the posts who are in Query 1 AND Query 2 .
I used wpdb instead of query_post();
A bit of code (returning published posts with category MyCat and tag 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);
This is a dirty way to do it but I hope it helps =)
-
使用[`WP_Query`和"tax_query` AND关系](http://codex.wordpress.org/Class_Reference/WP_Query#Taxonomy_Parameters)可以轻松完成此操作,无需使用原始SQL.This is much more easily accomplished with [`WP_Query` and a `tax_query` AND relationship](http://codex.wordpress.org/Class_Reference/WP_Query#Taxonomy_Parameters), no need for raw SQL.
- 7
- 2014-06-08
- Milo
-
绝对不需要在WordPress中进行原始查询即可实现此目的.There is absolutely no need to do raw queries in WordPress to achieve this.
- 0
- 2020-01-30
- Drmzindec
-
- 2016-03-11
此代码有效:
$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 );
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 );
-
- 2016-03-15
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"
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"
-
仅代码回答很少符合质量标准.请编辑您的答案,并附上注释/评论,说明如何解决原始问题以及实施方法/在何处实施.Code only answers rarely meet quality standards. Please edit your answer and include notes / commentary on how this solves the original issue along with how / where to implement it.
- 2
- 2016-03-15
- Howdy_McGee
-
使用WP_Query和tax_query AND关系可以轻松完成此操作,而无需原始SQL.This is much more easily accomplished with WP_Query and a tax_query AND relationship, no need for raw SQL.
- 0
- 2020-01-30
- Drmzindec
我正在尝试显示与类别X和标签Y相关的帖子列表. 我尝试了以下代码:
但是它不能正常工作,并返回该co \ category中的所有帖子.
很想听听您可能有的见识