Elencare tutti i post di un tipo di post personalizzato per tassonomia

25 set 2012, 17:24:45
Visualizzazioni: 137K
Voti: 33

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.

0
Tutte le risposte alla domanda 5
3
68

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!

25 set 2012 19:10:39
Commenti

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.

Rodrigo Zuluaga Rodrigo Zuluaga
7 mar 2017 16:00:57

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

GhostToast GhostToast
7 mar 2017 20:04:35

L'ho risolto in modo un po' diverso credo, ma il tuo codice è ottimo $args = array('post_type' => 'publica', 'tax_query' => array( array( 'taxonomy' => 'comision-publicaciones', 'field' => 'name', 'terms' => array($ter_name) ), ), );

Rodrigo Zuluaga Rodrigo Zuluaga
8 mar 2017 05:43:41
0

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;
}
25 set 2012 18:55:17
0

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.

10 lug 2014 15:16:29
1

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 } ?>
14 giu 2020 15:38:02
Commenti

oddio grazie! Avrei dovuto provare prima questa soluzione.

Mike Mike
15 set 2021 13:29:46
0
-1

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
29 lug 2014 08:58:25