Query dei post per nome del termine della tassonomia

6 set 2012, 13:18:21
Visualizzazioni: 23.6K
Voti: 2

Vorrei ottenere la lista dei post filtrati per nome del loro termine di tassonomia personalizzata (=store).

Questo è quello che ho finora, ma non funziona.

Per favore aiutatemi con il codice.

$mystorename è una variabile che contiene il nome del negozio per cui voglio eseguire la query.

Ogni aiuto sarà apprezzato.

Grazie

$args = array(
    'tax_query' => array(
    array(
        'taxonomy' => 'store', // tassonomia 'negozio'
        'field' => 'name', // filtra per nome
        'terms' => $mystorename // nome del termine da cercare
    )
)
);

$postslist = get_posts( $args );if(count($postslist) > 0){ ?>
0
Tutte le risposte alla domanda 3
1

Non sono sicuro che la funzione get_posts supporti il tax_query. Potresti provare a creare un nuovo oggetto WP_Query invece.

$args = array(
'tax_query' => array(
    array(
        'taxonomy' => 'store', // Tassonomia
        'field' => 'name',     // Campo
        'terms' => $mystorename // Termini da cercare
        )
    )
);

$query = new WP_Query($args);
if ( $query -> have_posts() ) : while ( $query -> have_posts() ) : $query -> the_post(); ?>
    <!-- post -->
<?php endwhile; ?>
    <!-- navigazione post -->
<?php else: ?>
    <!-- nessun post trovato -->
<?php endif; ?>
6 set 2012 18:16:16
Commenti

La query che hai pubblicato sopra continuava a darmi un errore ("expected )") sulla riga dell'ultimo "array(". Non avevo idea di cosa fosse sbagliato, così l'ho abbandonata e sono passato al codice qui sotto, che funziona perfettamente. Grazie

Naijadeals Naijadeals
8 set 2012 04:20:27
0
<?php
 $args=array(
  'store' => $mystoreslug,
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => -1,
'caller_get_posts'=> 1
);
$my_query = null;
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {?>

 <div class="itembox">
 <h1>Coupon del Negozio</h1>

<div class="itemboxinner">

 <?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
   <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Link permanente a <?php         the_title_attribute(); ?>"><h3><?php the_title(); ?></h3></a></p>
  <?php
 endwhile;
}
wp_reset_query();  
?>
8 set 2012 04:24:35
2

Che ne dici di...

$args = array(
    'store' => $mystorename, // memorizza il nome del negozio
);
$postslist = query_posts( $args ); // esegue la query per ottenere i post
wp_reset_query(); // reimposta la query
7 dic 2012 17:33:01
Commenti

Spiega perché ciò potrebbe risolvere il problema.

fuxia fuxia
7 dic 2012 17:44:19

Query posts dovrebbe essere usato come ultima risorsa, utilizza get_posts invece. $mystorename dovrebbe essere uno slug, un id o un nome visualizzato? Spiega

AlxVallejo AlxVallejo
17 dic 2012 19:16:40