Wie kann man Beiträge nach Kategorie und Tag abfragen?
-
-
Ich denke,mit query_posts () können Sienur Kategorie oder Tag verwenden.Ichbin mirnicht sicher,aber vielleichtist die Verwendung der Funktion auf dasbeschränkt,wasbedeuten würde,dass dies korrektfunktioniert,aberesmachtnicht das,was Sie wollen.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 Antworten
- Stimmen
-
- 2010-12-03
Bearbeiten: Im Folgendenerfahren Sie,wie Sie Kategorien- und Tag-Schnittpunkte richtig abfragen können.
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
Ich denke,diesistein Fehlerin WordPress,der an anderer Stelle kommentiert wurde. Versuchen Sie,den Namen des Tags anstelle der ID zu verwenden,dann sollteesfunktionieren:
$args = array( 'posts_per_page' => 3, 'tag' => 'review', 'cat' => 9, ); query_posts($args);
Lassen Sie uns wissen,wiees Ihnengeht,und wissen Sienicht,wasmit Tagsmit mehreren Wörternim Namenpassiert.
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
Ichbin auf dasselbe Problemgestoßen und habees durcheine MySQL-Anfragebehoben.
kurz: get_post ($ args)gibt Ihnen Beiträge zurück,die die Kategorie=MyCategory ODER das Tag=MyTag haben.
Siemöchten Ihr ODER in UND ändern.
Meine Logik war,direktmit einer MySQL-Abfragefortzufahren:
- Abfrage 1=Wählen Sie alle Beiträgemit der Kategorie MyCat aus
- Abfrage 2=Wählen Sie alle Beiträgemit dem Tag MyTag aus
- Endlich: Wählen Sie alle Beiträge aus,die sichin Abfrage 1 UND Abfrage 2befinden.
Ich habe wpdb anstelle von query_post ();
verwendetEinbisschen Code (Rückgabe veröffentlichter Beiträgemit der Kategorie MyCat und dem 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);
Diesistein schmutziger Weg,aberich hoffe,es hilft=)
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 =)
-
Diesistmit [`WP_Query` undeiner`tax_query` AND-Beziehung] (http://codex.wordpress.org/Class_Reference/WP_Query#Taxonomy_Parameters) vieleinfacher zuerreichen,ohne dass Raw SQLerforderlichist.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
-
Esist absolutnichterforderlich,rohe Abfragenin WordPress durchzuführen,um dies zuerreichen.There is absolutely no need to do raw queries in WordPress to achieve this.
- 0
- 2020-01-30
- Drmzindec
-
- 2016-03-11
Dieser Codefunktioniert:
$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"
-
Nur Code-Antwortenerfüllen selten Qualitätsstandards.Bittebearbeiten Sie Ihre Antwort undgeben Sie Hinweise/Kommentare dazu an,wie das ursprüngliche Problemgelöst wird und wie/woesimplementiert werden soll.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
-
Diesistmit WP_Query undeinertax_query AND-Beziehung vieleinfacher zuerreichen,ohne dass unformatiertes SQLerforderlichist.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
Ich versuche,eine Liste von Posts anzuzeigen,die sich auf Kategorie X und Tag Ybeziehen. Ich habe denfolgenden Code ausprobiert:
aberesfunktioniertnicht richtig undgibt alle Beiträgein der Kategorie zurück.
Würdegerneeinen Einblick hören,den Sie haben könnten