카테고리 및 태그별로 게시물을 쿼리하는 방법은 무엇입니까?
-
-
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)는 category=MyCategory 또는 tag=MyTag
인 게시물을 반환합니다.원하는 것은 OR 을 AND 로 변경하는 것입니다.
내 논리는 MySQL 쿼리로 바로가는 것이 었습니다.
- 쿼리 1=카테고리가 MyCat 인 모든 게시물 선택
- 쿼리 2=MyTag 태그가있는 모든 게시물 선택
- 마지막으로 : 쿼리 1 및 쿼리 2에있는 모든 게시물을 선택합니다.
query_post (); 대신 wpdb 를 사용했습니다.
일부 코드 (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 =)
-
이것은 원시 SQL이 필요없는 [`WP_Query` 및`tax_query` AND 관계] (http://codex.wordpress.org/Class_Reference/WP_Query#Taxonomy_Parameters)를 사용하면 훨씬 더 쉽게 수행 할 수 있습니다.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 \ ategory의 모든 게시물을 반환합니다.
통찰을 듣고 싶습니다.