Elencare tutti i post di un tipo di post personalizzato per tassonomia
C'è un modo per elencare tutti i post di uno specifico tipo di post personalizzato e organizzarli in base al termine di tassonomia personalizzata ad essi associato?
Per esempio:
Termine Tassonomia #1
Tipo di Post
Tipo di Post
Tipo di Post
Termine Tassonomia #2
Tipo di Post
Tipo di Post
Qualsiasi aiuto sarebbe molto apprezzato.
Grazie.

Prova questo
$custom_terms = get_terms('custom_taxonomy');
foreach($custom_terms as $custom_term) {
wp_reset_query();
$args = array('post_type' => 'custom_post_type',
'tax_query' => array(
array(
'taxonomy' => 'custom_taxonomy',
'field' => 'slug',
'terms' => $custom_term->slug,
),
),
);
$loop = new WP_Query($args);
if($loop->have_posts()) {
echo '<h2>'.$custom_term->name.'</h2>';
while($loop->have_posts()) : $loop->the_post();
echo '<a href="'.get_permalink().'">'.get_the_title().'</a><br>';
endwhile;
}
}
Otteniamo tutti i termini di una tassonomia, li iteriamo e mostriamo un titolo con link per ogni post che appartiene a quel termine. Se hai bisogno di riordinare i termini della tassonomia, puoi farlo facilmente con un plugin. Reorder Taxonomy, credo. Ma fai attenzione che questo plugin aggiunge(!) un'altra colonna alla tua tabella all'attivazione e non la rimuove alla disattivazione!

Ciao @GhostToast Funziona alla grande, ho una domanda diretta, come posso filtrare per tassonomia? Ho tennis, golf, calcio, pallavolo, questo codice li mostra tutti con i loro post che hanno la tassonomia selezionata. Come posso filtrare per mostrare solo la tassonomia Calcio con i suoi post.

@RodrigoZuluaga sarebbe allora una semplice query singola. Togli $custom_terms
e il foreach()
e definisci manualmente 'terms'
con lo slug o quello che preferisci.

Non è una soluzione particolarmente elegante, ma puoi creare query multiple, ognuna per termini specifici, e poi visualizzarle. Speriamo che qualcuno possa trovare un modo più carino per estrarre automaticamente i termini e modificare l'output/ordinamento. Ma questo ti permetterebbe di iniziare.
<?php
// Prima Query per i post che corrispondono a term1
$args = array(
'tax_query' => array(
array(
'taxonomy' => 'taxonomy_1',
'field' => 'slug',
'terms' => array( 'term1' )
),
),
'post_type' => 'my-post-type'
);
$query = new WP_Query( $args );
if ( have_posts() ) {
$term = $query->queried_object;
echo 'Tutti i post trovati in ' . $term->name;
while ( have_posts() ) : the_post();
// Visualizza ciò che desideri
the_title();
the_content();
endwhile;
}
// RESET DELLE VARIABILI DELLA QUERY
wp_reset_query();
// Seconda Query per term2
$args = array(
'tax_query' => array(
array(
'taxonomy' => 'taxonomy_1',
'field' => 'slug',
'terms' => array( 'term2' )
),
),
'post_type' => 'my-post-type'
);
$query = new WP_Query( $args );
if ( have_posts() ) {
$term = $query->queried_object;
echo 'Tutti i post trovati in ' . $term->name;
while ( have_posts() ) : the_post();
// Visualizza ciò che desideri
the_title();
the_content();
endwhile;
}

Ottimo! La soluzione di GhostOne era esattamente quello che stavo cercando. Nella mia situazione il custom post type era 'minining_accidents' e le tassonomie personalizzate associate erano 'accident-types' che contenevano diversi termini al loro interno. La mia idea era creare un widget personalizzato per mostrare l'elenco degli articoli sotto i termini di queste tassonomie personalizzate. Durante il test ho ottenuto ciò che volevo. Il resto era solo rifinire. Ecco il mio codice:
function fn_get_list_of_mining_accident_types()
{
$custom_taxonomy='accident-types';
$custom_terms = get_terms($custom_taxonomy);
$str_return='<ul>';
foreach($custom_terms as $custom_term)
{
wp_reset_query();
$args = array(
'post_type' => 'minining_accidents',
'tax_query' => array(
array(
'taxonomy' => $custom_taxonomy,
'field' => 'slug',
'terms' => $custom_term->slug,
),
),
);
$loop = new WP_Query($args);
$term_name=$custom_term->name;
$term_slug=$custom_term->slug;
$term_link=get_term_link($term_slug, $custom_taxonomy);
$str_return.='<li><a href="'.$term_link.'">'.$term_name.'</a>';
if($loop->have_posts())
{
$str_return.='<ol>';
while($loop->have_posts()) : $loop->the_post();
$str_return.='<li><a href="'.get_permalink().'">'.get_the_title().'</a></li> ';
endwhile;
$str_return.='</ol>';
}
$str_return.='</li>';
}
$str_return.='</ul>';
return $str_return;
}
Sì! C'è sempre la possibilità di migliorare ulteriormente il codice.

Questa è la soluzione lunga che ho provato prima di arrivare a questo thread. Spero che possa aiutare qualcuno a capire meglio.
<?php
// Ottieni la lista di tutti i termini della tassonomia -- In parole semplici, i titoli delle categorie
$args = array(
'taxonomy' => 'project_category',
'orderby' => 'name',
'order' => 'ASC'
);
$cats = get_categories($args);
// Per ogni termine della tassonomia personalizzata, ottieni i post tramite term_id
foreach($cats as $cat) {
?>
<a href="<?php echo get_category_link( $cat->term_id ) ?>">
<?php echo $cat->name; ?> <br>
<?php // echo $cat->term_id; ?> <br>
</a>
<?php
// Argomenti della query
$args = array(
'post_type' => 'portfolio', // il tipo di post
'tax_query' => array(
array(
'taxonomy' => 'project_category', // il vocabolario personalizzato
'field' => 'term_id', // term_id, slug o name (Definisci con cosa vuoi cercare il termine sotto)
'terms' => $cat->term_id, // fornisci gli slug dei termini
),
),
);
// La query
$the_query = new WP_Query( $args );
// Il Loop
if ( $the_query->have_posts() ) {
echo '<h2> Lista dei post con questo tag </h2>';
echo '<ul>';
$html_list_items = '';
while ( $the_query->have_posts() ) {
$the_query->the_post();
$html_list_items .= '<li>';
$html_list_items .= '<a href="' . get_permalink() . '">';
$html_list_items .= get_the_title();
$html_list_items .= '</a>';
$html_list_items .= '</li>';
}
echo $html_list_items;
echo '</ul>';
} else {
// nessun post trovato
}
wp_reset_postdata(); // resetta il global $post;
?>
<?php } ?>

Per mostrare una lista di post personalizzati da una tassonomia personalizzata
$args = array(
'tax_query' => array(
array(
'taxonomy' => 'your-custom-taxonomy',
'field' => 'slug',
'terms' => array( 'your-term' )
),
),
'post_type' => 'your-post-type'
);
$loop = new WP_Query($args);
if($loop->have_posts()) {
$term = $wp_query->queried_object;
while($loop->have_posts()) : $loop->the_post();
//Output ciò che desideri
echo '<li><a href="'.get_permalink().'">'.get_the_title().'</a></li>';
endwhile;
}
Titolo
- Elemento lista
- Elemento lista
- Elemento lista
