Ottieni Post per Custom Post Type, Tassonomia e Termine

14 mar 2012, 19:43:14
Visualizzazioni: 131K
Voti: 19

Ok, ho un Custom Post Type chiamato "Services". Questo custom post type ha una tassonomia chiamata "Areas" e ci sono 5 termini in quella tassonomia.

Mettiamo che ho 10 post in "Services" e ci sono 5 post nel termine "Painting" e altri 5 nel termine "Photography".

Ho bisogno di poter interrogare i post da "Services" ma invece di mostrare tutti e 10 i post, mostrare solo i 5 associati a "Painting".

Al momento sono in grado di interrogare per tassonomia e termini, ma questo mostrerà tutti i post da "services" senza filtro per termine.

In pratica, devo interrogare i post per post_type dal termine che scelgo.

Qualsiasi aiuto sarebbe fantastico. Grazie.

<ul id="service-list">
<?php 
        $args = array('tax_query' => array( array('taxonomy' => 'areas', 'field' => 'slug','terms' => 'painting')));

        $the_query = new WP_Query( $args );

        if($the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post();

        ?>

    <li class="service">
        <h2><?php the_title(); ?></h2>
        <?php the_content(); ?>
    </li><!-- /.service -->

<?php endwhile; else: ?>

    <p>Nessun contenuto qui.</p>

<?php endif; wp_reset_postdata(); ?>

</ul><!-- #service-list -->

Quindi se potessi solo specificare negli $args da quale post type ottenere i post, questo sarebbe risolto.

3
Commenti

Ok, sembra che abbia pensato troppo a tutto questo e la soluzione era davvero semplice:

Rise Rise
14 mar 2012 19:55:34

Per favore segna la tua risposta come corretta o elimina il topic.

AlxVallejo AlxVallejo
14 mar 2012 19:57:22

Devo aspettare 7 ore per postare la soluzione :(

Rise Rise
14 mar 2012 19:58:28
Tutte le risposte alla domanda 1
0
36

Questa è la risposta alla domanda :)

<?php 

$args = array(
    'post_type'=> 'services',
    'areas'    => 'painting',
    'order'    => 'ASC'
);              

$the_query = new WP_Query( $args );
if($the_query->have_posts() ) : 
    while ( $the_query->have_posts() ) : 
       $the_query->the_post(); 
       // il contenuto va qui
    endwhile; 
    wp_reset_postdata(); 
else: 
endif;

?>
22 mar 2012 17:25:04