Alle Beiträge in einem benutzerdefinierten Beitragstyp anzeigen, gruppiert nach einer benutzerdefinierten Taxonomie
7 Antworten
- Stimmen
-
- 2011-11-04
Ich habeeine Lösunggefunden,indemicheine benutzerdefinierte Abfrage verwendet und sie dannmit dem Begriff Namengruppiert habe:
SELECT * FROM wp_term_taxonomy AS cat_term_taxonomy INNER JOIN wp_terms AS cat_terms ON cat_term_taxonomy.term_id = cat_terms.term_id INNER JOIN wp_term_relationships AS cat_term_relationships ON cat_term_taxonomy.term_taxonomy_id = cat_term_relationships.term_taxonomy_id INNER JOIN wp_posts AS cat_posts ON cat_term_relationships.object_id = cat_posts.ID INNER JOIN wp_postmeta AS meta ON cat_posts.ID = meta.post_id WHERE cat_posts.post_status = 'publish' AND meta.meta_key = 'active' AND meta.meta_value = 'active' AND cat_posts.post_type = 'member' AND cat_term_taxonomy.taxonomy = 'member_groups'
Dann kannichmit einer regulärenforeach-Abfrageeinfach diegewünschten Informationenextrahieren.
Aberichbin immernoch aneiner anderen Möglichkeitinteressiert,wenneseine gibt,vielleicht durch die Verwendung dereigenen Funktionen von Wordpress.
I found a solution by using a custom query and then grouping it with the term name:
SELECT * FROM wp_term_taxonomy AS cat_term_taxonomy INNER JOIN wp_terms AS cat_terms ON cat_term_taxonomy.term_id = cat_terms.term_id INNER JOIN wp_term_relationships AS cat_term_relationships ON cat_term_taxonomy.term_taxonomy_id = cat_term_relationships.term_taxonomy_id INNER JOIN wp_posts AS cat_posts ON cat_term_relationships.object_id = cat_posts.ID INNER JOIN wp_postmeta AS meta ON cat_posts.ID = meta.post_id WHERE cat_posts.post_status = 'publish' AND meta.meta_key = 'active' AND meta.meta_value = 'active' AND cat_posts.post_type = 'member' AND cat_term_taxonomy.taxonomy = 'member_groups'
Then by just using a regular foreach query I can just extract the information I want.
But I'm still interested in another way if there is, maybe by using Wordpress' own functions.
-
Ich habegeradeeine alternative Methode hinzugefügt.Ichneige dazu,alles zu scheuen,was rohe SQL-Abfragenerfordert.I've just added an alternative method. I tend to shy away from anything that requires raw SQL queries.
- 0
- 2012-01-25
- Chip Bennett
-
Ichbin froh,dass dies als die richtige Antwortmarkiertist,auch wenn die Abfragein WordPressnichtmehrfunktioniert,wenn sich das Schemairgendwann ändert ... Das Konzept,sie allein einereinzigen Abfrage zu sammeln,ist die richtige Antwort.Die Iteration zur Gruppierung der Taxonomienin PHP wirdnicht annähernd sogut skalieren wie dies der Fall sein wird.I'm glad to see this marked as the correct answer, even if the query stops working in wordpress if the schema changes at some point... The concept of collecting them all in a single query is the correct answer. Iteration to group the taxonomies in php wont scale nearly as well as this will.
- 3
- 2013-11-12
- wowo_999
-
OMG,schreiben Sieernsthaft Rohanfragen?Wahrscheinlich sind Siegeradein die WP-Weltgesprungen oder haben sichnie die Mühegemacht,ihren Standards zufolgen.Die Antwort solltenicht ausgewählt werden.OMG, Are you serious writing raw query? Probably, you have just jumped into the WP world or never bother following its standards. The answer should not be selected.
- 0
- 2020-07-01
- pixelngrain
-
- 2012-01-25
Sie könnten alsoin Betracht ziehen,diemehreren Abfragen zu automatisieren.
Rufen Sie zunächst die Liste der Begriffein Ihrerbenutzerdefinierten Taxonomiemit
get_terms()
ab :<?php $member_group_terms = get_terms( 'member_group' ); ?>
Führen Sie danneine Schleife durch undführen Siejedes Maleine neue Abfrage aus:
<?php foreach ( $member_group_terms as $member_group_term ) { $member_group_query = new WP_Query( array( 'post_type' => 'member', 'tax_query' => array( array( 'taxonomy' => 'member_group', 'field' => 'slug', 'terms' => array( $member_group_term->slug ), 'operator' => 'IN' ) ) ) ); ?> <h2><?php echo $member_group_term->name; ?></h2> <ul> <?php if ( $member_group_query->have_posts() ) : while ( $member_group_query->have_posts() ) : $member_group_query->the_post(); ?> <li><?php echo the_title(); ?></li> <?php endwhile; endif; ?> </ul> <?php // Reset things, for good measure $member_group_query = null; wp_reset_postdata(); } ?>
Ich kannbei diesem Ansatznichtsbesonders Falsches sehen,obwohlermöglicherweisenureingeschränkt skalierbarist (dh wenn Sie Hunderte oder Tausende von Mitgliedern oder Mitgliedergruppenbegriffe haben,wirdmöglicherweise die Leistung angezeigt Probleme).
So, you might consider automating the multiple queries.
First, get the list of terms in your custom taxonomy, using
get_terms()
:<?php $member_group_terms = get_terms( 'member_group' ); ?>
Then, loop through each one, running a new query each time:
<?php foreach ( $member_group_terms as $member_group_term ) { $member_group_query = new WP_Query( array( 'post_type' => 'member', 'tax_query' => array( array( 'taxonomy' => 'member_group', 'field' => 'slug', 'terms' => array( $member_group_term->slug ), 'operator' => 'IN' ) ) ) ); ?> <h2><?php echo $member_group_term->name; ?></h2> <ul> <?php if ( $member_group_query->have_posts() ) : while ( $member_group_query->have_posts() ) : $member_group_query->the_post(); ?> <li><?php echo the_title(); ?></li> <?php endwhile; endif; ?> </ul> <?php // Reset things, for good measure $member_group_query = null; wp_reset_postdata(); } ?>
I can't see anything particularly wrong with this approach, though it may have a limited ability to scale (i.e. if you have hundreds or thousands of members, or member_group terms, you may see performance issues).
-
Ja,esfunktioniertperfekt. Nurein Problem,dasich habe. Ichmöchte Cutom-Felder wie diese Phpget_post_meta ($member_group_term-> ID,'job_title',true) anzeigen.?>,Aberes hatnichtfunktioniert. Ich habees auchmit $post- versucht> ID,aberes hatnichtfunktioniert. Könnten Siebitte @Chip Bennett helfen?Yes it wordks perfectly.Only one issue i have.I wanna display cutom fields like this ID, 'job_title', true);?> but it didnt work.I also tried with $post->ID but no didnbt work,could you help @Chip Bennett please?
- 0
- 2015-12-21
- Anahit DEV
-
- 2012-04-04
nocheinfacher:
$terms = get_terms('tax_name'); $posts = array(); foreach ( $terms as $term ) { $posts[$term->name] = get_posts(array( 'posts_per_page' => -1, 'post_type' => 'post_type', 'tax_name' => $term->name )); }
Innerhalb des resultierenden $posts-Arraysistjeder Steuerbegriff der Schlüssel zueinem verschachtelten Array,das seine Postsenthält.
even simpler:
$terms = get_terms('tax_name'); $posts = array(); foreach ( $terms as $term ) { $posts[$term->name] = get_posts(array( 'posts_per_page' => -1, 'post_type' => 'post_type', 'tax_name' => $term->name )); }
Within the resultant $posts array, each tax term is the key to a nested array containing its posts.
-
- 2013-01-24
Ich hattegenau dieses Bedürfnis und Chips Lösung funktionierte,abgesehen voneiner Sache:
'field' => 'slug'
isterforderlich.foreach ( $service_categories as $category ) { $services = new WP_Query( array( 'post_type' => 'service', 'tax_query' => array( array( 'taxonomy' => 'service_category', 'terms' => array( $category->slug ), 'operator' => 'IN', 'get' => 'all', 'field' => 'slug' ) ) ) ); ?> <h2><?php echo $category->slug; ?></h2> <?php if ( $services->have_posts() ) { // loop stuff goes here ?>
Die resultierende Anzeigemusste auchflach sein,also
'get' => 'all'
ist hiereingestellt.Hoffentlich hilft dasjemand anderem.
I had this exact need, and Chip's solution worked, except for one thing:
'field' => 'slug'
is required.foreach ( $service_categories as $category ) { $services = new WP_Query( array( 'post_type' => 'service', 'tax_query' => array( array( 'taxonomy' => 'service_category', 'terms' => array( $category->slug ), 'operator' => 'IN', 'get' => 'all', 'field' => 'slug' ) ) ) ); ?> <h2><?php echo $category->slug; ?></h2> <?php if ( $services->have_posts() ) { // loop stuff goes here ?>
I also needed the resulting display to be flat, so
'get' => 'all'
is set here.Hopefully this helps somebody else out.
-
- 2011-11-05
$query = new WP_Query( array ( 'post_type' => 'member', 'orderby' => 'meta_value', 'meta_key' => 'member_group' ) );
Wenn Sie diese Abfrage durchlaufen,können Sieeinfachein ifin dieser Richtung verwenden (im PHP-Pseudocode)
$groupName = ""; $counter = 0; if havePosts: while havePosts: thePost if( $groupName != post->meta_value ) { if ($counter > 0) { </ul> } <h1>A group name</h1> <ul> <li>member name</li> } else { <li>member name</li> } endwhile;endif </ul>
Ich hoffe das hilft.Ich denke,Sie haben das weitaus kompliziertergemacht,alses seinmusste.
Weitere Informationen: http://codex.wordpress.org/Class_Reference/WP_Query#Taxonomy_Parameters
$query = new WP_Query( array ( 'post_type' => 'member', 'orderby' => 'meta_value', 'meta_key' => 'member_group' ) );
Then when you loop through this query you could just use an if along these lines (in php pseudocode)
$groupName = ""; $counter = 0; if havePosts: while havePosts: thePost if( $groupName != post->meta_value ) { if ($counter > 0) { </ul> } <h1>A group name</h1> <ul> <li>member name</li> } else { <li>member name</li> } endwhile;endif </ul>
I hope that helps. I think you were making this far more complicated than it needed to be.
More information: http://codex.wordpress.org/Class_Reference/WP_Query#Taxonomy_Parameters
-
- 2015-02-11
Ichmusste dies vor Jahrenbei einem Projekttun. Ähnliche Antwort auf DJB,nurmit einbisschenmehr Details. Dadurch werden alle Ihre Taxonomienamen als h3 ausgegeben,wobeieine Aufzählungsliste aller Post-Titelmit ihrer Detailseite verknüpftist.
<?php // Output all Taxonomies names with their respective items $terms = get_terms('member_groups'); foreach( $terms as $term ): ?> <h3><?php echo $term->name; // Print the term name ?></h3> <ul> <?php $posts = get_posts(array( 'post_type' => 'member', 'taxonomy' => $term->taxonomy, 'term' => $term->slug, 'nopaging' => true, // to show all posts in this taxonomy, could also use 'numberposts' => -1 instead )); foreach($posts as $post): // begin cycle through posts of this taxonmy setup_postdata($post); //set up post data for use in the loop (enables the_title(), etc without specifying a post ID) ?> <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li> <?php endforeach; ?> </ul> <?php endforeach; ?>
I had to do this on a project years ago. Similar answer to djb, just with a bit more details. This will output all of your taxonomy names as an h3, with a bulleted list of each post title linked to their detail page.
<?php // Output all Taxonomies names with their respective items $terms = get_terms('member_groups'); foreach( $terms as $term ): ?> <h3><?php echo $term->name; // Print the term name ?></h3> <ul> <?php $posts = get_posts(array( 'post_type' => 'member', 'taxonomy' => $term->taxonomy, 'term' => $term->slug, 'nopaging' => true, // to show all posts in this taxonomy, could also use 'numberposts' => -1 instead )); foreach($posts as $post): // begin cycle through posts of this taxonmy setup_postdata($post); //set up post data for use in the loop (enables the_title(), etc without specifying a post ID) ?> <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li> <?php endforeach; ?> </ul> <?php endforeach; ?>
-
- 2017-04-22
Nun,esistein alter Thread,aber wennjemand wieich vorbeikommt,könnte dies helfen. Die Ideeist,die Hauptabfrage so zu ändern,dass wirnicht die Vorlagen aufrufen undneue Abfragen und Schleifengenerierenmüssen ...
PS: Nochnichtin großen DBSgetestet. Inmeinem Fall wares zufriedenstellend.
function grouped_by_taxonomy_main_query( $query ) { if ( $query->is_home() && $query->is_main_query() ) { // Run only on the homepage $post_ids = array(); $terms = get_terms('my_custom_taxonomy'); foreach ( $terms as $term ) { $post_ids = array_merge( $post_ids, get_posts( array( 'posts_per_page' => 4, // as you wish... 'post_type' => 'my_custom_post_type', // If needed... Default is posts 'fields' => 'ids', // we only want the ids to use later in 'post__in' 'tax_query' => array( array( 'taxonomy' => $term->taxonomy, 'field' => 'term_id', 'terms' => $term->term_id, )))) // getting posts in the current term ); } $query->query_vars['post_type'] = 'my_custom_post_type'; // Again, if needed... Default is posts $query->query_vars['posts_per_page'] = 16; // If needed... $query->query_vars['post__in'] = $post_ids; // Filtering with the post ids we've obtained above $query->query_vars['orderby'] = 'post__in'; // Here we keep the order we generated in the terms loop $query->query_vars['ignore_sticky_posts'] = 1; // If you dont want your sticky posts to change the order } } // Hook my above function to the pre_get_posts action add_action( 'pre_get_posts', 'grouped_by_taxonomy_main_query' );
Well, it's an old thread, but if someone passes by as I did, this might help. The idea is to modify the main query so we don't need to go the templates and generate new queries and loops...
PS: Yet to be tested in large dbs. It was satisfactory in my case.
function grouped_by_taxonomy_main_query( $query ) { if ( $query->is_home() && $query->is_main_query() ) { // Run only on the homepage $post_ids = array(); $terms = get_terms('my_custom_taxonomy'); foreach ( $terms as $term ) { $post_ids = array_merge( $post_ids, get_posts( array( 'posts_per_page' => 4, // as you wish... 'post_type' => 'my_custom_post_type', // If needed... Default is posts 'fields' => 'ids', // we only want the ids to use later in 'post__in' 'tax_query' => array( array( 'taxonomy' => $term->taxonomy, 'field' => 'term_id', 'terms' => $term->term_id, )))) // getting posts in the current term ); } $query->query_vars['post_type'] = 'my_custom_post_type'; // Again, if needed... Default is posts $query->query_vars['posts_per_page'] = 16; // If needed... $query->query_vars['post__in'] = $post_ids; // Filtering with the post ids we've obtained above $query->query_vars['orderby'] = 'post__in'; // Here we keep the order we generated in the terms loop $query->query_vars['ignore_sticky_posts'] = 1; // If you dont want your sticky posts to change the order } } // Hook my above function to the pre_get_posts action add_action( 'pre_get_posts', 'grouped_by_taxonomy_main_query' );
Ich arbeite aneiner Mitgliederseite,auf dericheinen benutzerdefinierten Beitragstypmit einerbenutzerdefinierten Taxonomie verwende.Meinbenutzerdefinierter Beitragstyp heißt
member
undmeine benutzerdefinierte Taxonomie heißtmember_groups
.Ichmöchte alle Mitglieder auflisten,aber siein ihrenjeweiligen Gruppen zusammenfassen.
Umes klar zu sagen,ich habe 35 Mitglieder,diein 9 Gruppen unterteilt sind. Anstatt dieselbe Abfrageneun Mal durchzuführen,möchteich sieeinmal ausführen,aber sie zusammengruppieren,sodass Mitglied1,Mitglied4 und Mitglied 11in gruppiert werdeneine Gruppenamens "Marketing".
Ich verwende
WP_Query
,um alle Beiträge unter dem Beitragstyp-Mitglied abzurufen.Ich habe verschiedene Versuche versucht,aber keinerfolgreiches Ergebnis.Wie kannich daserreichen?