Query di tutti i post con un tag specifico

15 ott 2014, 12:30:59
Visualizzazioni: 47K
Voti: 4

Ho bisogno di recuperare tutti i post con un tag specifico, ma sto ottenendo tutti i post invece. La mia query funziona se pubblico un post con il tag di cui ho bisogno ed elenco tutti i post con quel tag, ma quando pubblico un post con un altro tag, recupera anche il post appena pubblicato.

Questa è la mia query:

$original_query = $wp_query;
$wp_query = null;
$args=array(
    'posts_per_page' => -1, // Recupera tutti i post
    'tag' => $post_tag
);
$wp_query = new WP_Query( $args );
$post_titles=array();
$i=0;
if ( have_posts() ) :
    while (have_posts()) : the_post();
        $post_titles[$i]=get_the_ID() ;
        $i++;
    endwhile;
endif;
$wp_query = null;
$wp_query = $original_query;
wp_reset_postdata();
tag
2
Commenti

Come stai ottenendo $post_tag. E la tua query è la stessa di query_posts che non dovrebbe essere utilizzato affatto.

Pieter Goosen Pieter Goosen
15 ott 2014 13:13:11

ho un tag specifico che ho usato staticamente

Antwan Antwan
15 ott 2014 13:19:03
Tutte le risposte alla domanda 2
5

È molto più semplice creare una nuova WP_Query che cercare di cancellare o sovrascrivere quella originale.

Se $post_tag è lo slug di un tag, potresti semplicemente usare:

<?php
$the_query = new WP_Query( 'tag='.$post_tag );

if ( $the_query->have_posts() ) {
    echo '<ul>';
    while ( $the_query->have_posts() ) {
        $the_query->the_post();
        echo '<li>' . get_the_title() . '</li>';
    }
    echo '</ul>';
} else {
    // nessun post trovato
}
/* Ripristina i dati originali del Post */
wp_reset_postdata();
15 ott 2014 12:45:11
Commenti

sì $post_tag è lo slug del tag ma non ho capito qual è la differenza fondamentale nel tuo codice scusa sono un principiante in wordpress

Antwan Antwan
15 ott 2014 12:58:46

La differenza principale è che il tuo codice cerca di annullare la Query principale di WP invece di crearne semplicemente una nuova.

Courtney Ivey Courtney Ivey
15 ott 2014 13:05:09

ottengo "nessun post trovato" mi dispiace ma c'è qualche differenza tra tag e tag slug?

Antwan Antwan
15 ott 2014 13:09:46

Nella Query, puoi utilizzare slug o ID.

Courtney Ivey Courtney Ivey
15 ott 2014 13:12:09

ora funziona ma ho aggiunto un nuovo tag in inglese e funziona, però ho tag in arabo e su quelli non funziona

Antwan Antwan
15 ott 2014 13:18:19
0

Nel tuo functions.php

/* Mostra Prodotti Correlati */
/* ======================== */

if ( ! function_exists( 'display_related_products' ) ) {

    function display_related_products($post_tag) {
        ?>
        <div class="related-products">

            <!-- semplice WP_Query -->
            <?php
                $args = array(
                    'post_type' => 'product',
                    'tag' => $post_tag, // Qui viene filtrato in base al tag che desideri
                    'orderby' => 'id',
                    'order' => 'ASC'
                );

                $related_products = new WP_Query( $args );
            ?>

            <?php while ( $related_products -> have_posts() ) : $related_products -> the_post(); ?>

                <a href="<?php the_permalink(); ?>" class="related-product">
                    <?php if( has_post_thumbnail() ) : ?>
                        <?php the_post_thumbnail( 'full', array( 'class' => 'related-product-img', 'alt' => get_the_title() ) ); ?>
                    <?php endif; ?>
                </a>

            <?php endwhile; wp_reset_query(); ?>

        </div>
        <?php
    }
}

Chiama da qualsiasi punto con

display_related_products('nome-del-tag');
5 ago 2016 00:20:54