Raggruppare WP_Query per categoria
OK, ecco la mia configurazione:
Un custom post type chiamato "issues" (per una rivista) Post con un campo personalizzato che corrisponde all'ID del numero della rivista collegato.
Quando sono nella pagina singola di un post "issue", voglio interrogare tutti i post correlati e mostrarli raggruppati per categoria associata. Ho la query dei post funzionante, ma non riesco a capire come fare il raggruppamento per categoria.
Ecco la mia query
<?php
global $post;
// Elenco post tramite i termini di una tassonomia personalizzata per qualsiasi post type
$current = get_the_ID($post->ID);
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => -1,
'orderby' => 'title',
'meta_key' => '_rkv_issue_select',
'meta_value' => $current
);
$issue_cats = new WP_Query($args);
if( $issue_cats->have_posts() ) :
?>
<ul>
<?php while ( $issue_cats->have_posts() ) : $issue_cats->the_post(); ?>
<li><?php the_title(); ?></li>
<?php endwhile; // fine del loop ?>
<?php else : ?>
<?php endif; // if have_posts() ?>
</ul>
<?php wp_reset_query(); ?>
Potresti considerare di modificare la WP_Query con un comando SQL per raggrupparli, ma questo va un po' oltre le mie attuali conoscenze di MySQL. Tuttavia, ho sempre fatto eseguendo un foreach sulla tassonomia stessa con questo http://codex.wordpress.org/Function_Reference/get_categories
Ecco un esempio di codice:
<?php
global $post;
$current = get_the_ID($post->ID);
$cargs = array(
'child_of' => 0,
'orderby' => 'name',
'order' => 'ASC',
'hide_empty' => 1,
'taxonomy' => 'category', //cambia questo con qualsiasi tassonomia
);
foreach (get_categories($cargs) as $tax) :
// Elenca i post in base ai termini per una tassonomia personalizzata di qualsiasi tipo di post
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => -1,
'orderby' => 'title',
'meta_key' => '_rkv_issue_select',
'meta_value' => $current,
'tax_query' => array(
array(
'taxonomy' => 'category',
'field' => 'slug',
'terms' => $tax->slug
)
)
);
if (get_posts($args)) :
?>
<h2><?php echo $tax->name; ?></h2>
<ul>
<?php foreach(get_posts($args) as $p) : ?>
<li><a href="<?php echo get_permalink($p); ?>"><?php echo $p->post_title; ?></a></li>
<?php endforeach; ?>
</ul>
<?php
endif;
endforeach;
?>
Questo codice scorrerà ogni categoria con post (hide_empty è impostato su true) ed esegue un get_posts su di essa (controllando anche che abbia post prima di produrre qualsiasi output).
Non ero sicuro di quale intestazione volessi per separare i raggruppamenti, quindi ho usato un h2 e ho aggiunto anche un link all'elenco.
Ho cambiato in get_posts perché l'ho trovato più efficiente in quanto non sovrascrive la variabile globale $post (meno chiamate al database, meno utilizzo di wp_reset_query()).

Questo è un modo piuttosto inefficiente di farlo, se hai molte categorie recuperate, poiché per ogni categoria fai una chiamata get_posts separata che interroga il database ogni volta.

Grazie per questa risposta, mi stavo scervellando per trovare un modo per raggruppare i risultati per tassonomie...
