Ripristinare i dati del post al loop precedente nei loop annidati

13 dic 2013, 11:33:10
Visualizzazioni: 14.9K
Voti: 25

Sto cercando di utilizzare loop annidati con il plugin posts to posts. Entrambi i loop funzionano, ma il problema sorge dopo il secondo loop annidato ($issue). Voglio accedere nuovamente al loop $publication, ma i dati sono ancora quelli di $issue.

wp_reset_query() ripristinerà direttamente il loop principale in single.php, cosa che non voglio.

Potrei usare get_posts() invece di new WP_Query, ma voglio poter utilizzare get_template_part().

Come posso ripristinare i dati al loop publication, in modo che il secondo 'Titolo pubblicazione' restituisca il titolo della pubblicazione e non dell'issue?

Ecco il mio codice all'interno di single.php:

$publication = new WP_Query( array(
'connected_type'  => 'publication_to_post',
'connected_items' => $post->ID,
'fields'          => 'ids',
'posts_per_page'  => 1,
) );

if ( $publication->have_posts() ) {
while ( $publication->have_posts() ) : $publication->the_post();
    echo '<h2>Titolo pubblicazione = '.get_the_title().'</h2>';
    $pub_id = get_the_ID();

    $issue = new WP_Query( array(
        'connected_type'  => 'publication_to_issue',
        'connected_items' => $pub_id,
        'fields'          => 'ids',
        'posts_per_page'  => 1,
    ) );

    if ( $issue->have_posts() ) {
        while ( $issue->have_posts() ) : $issue->the_post();

            // è necessario poter utilizzare parti di template qui
            echo '<h2>Titolo issue = '.get_the_title().'</h2>';

        endwhile;
    }

    // Attualmente restituisce il titolo dell'issue, non della pubblicazione
    echo '<h2>Titolo pubblicazione = '.get_the_title().'</h2>';

endwhile;
}
0
Tutte le risposte alla domanda 2
2
23

Risponderò io stesso, ma è stato il molto intelligente @simonwheatley di Code for the People a risolvere questo problema per me.

Invece di usare wp_reset_postdata() o wp_reset_query(), puoi usare il seguente:

$publication->reset_postdata();

Dove $publication è il tuo oggetto di query.

Il codice funzionante ora appare così:

$publication = new WP_Query( array(
'connected_type'  => 'publication_to_post',
'connected_items' => $post->ID,
'fields'          => 'ids',
'posts_per_page'  => 1,
) );

if ( $publication->have_posts() ) {
while ( $publication->have_posts() ) : $publication->the_post();
    echo '<h2>Titolo pubblicazione = '.get_the_title().'</h2>';
    $pub_id = get_the_ID();

    $issue = new WP_Query( array(
        'connected_type'  => 'publication_to_issue',
        'connected_items' => $pub_id,
        'fields'          => 'ids',
        'posts_per_page'  => 1,
    ) );

    if ( $issue->have_posts() ) {
        while ( $issue->have_posts() ) : $issue->the_post();

            // è necessario poter utilizzare parti di template qui
            echo '<h2>Titolo numero = '.get_the_title().'</h2>';

        endwhile; $publication->reset_postdata();
    }

    echo '<h2>Titolo pubblicazione = '.get_the_title().'</h2>';

endwhile;
}
13 dic 2013 12:04:25
Commenti

In effetti, questo è il modo molto più intelligente per farlo.

David David
13 dic 2013 12:16:53

Funziona davvero per te?

GDY GDY
6 mar 2018 15:36:08
0

Innanzitutto, penso sia possibile utilizzare get_posts() in combinazione con setup_postdata(). Con queste funzioni, puoi utilizzare i tag del template come in un normale loop di WordPress.

Ma puoi anche usare questa funzione nei tuoi loop annidati:

# assicurati che $post sia globale nel tuo scope (dovrebbe esserlo in single.php)
global $post;
if ( $publication->have_posts() ) {
while ( $publication->have_posts() ) : $publication->the_post();
    echo '<h2>Titolo pubblicazione = '.get_the_title().'</h2>';
    $pub_id = get_the_ID();

    # preserva il post corrente nel loop superiore
    $preserve_post = get_post();

    $issue = new WP_Query( array(
        'connected_type'  => 'publication_to_issue',
        'connected_items' => $pub_id,
        'fields'          => 'ids',
        'posts_per_page'  => 1,
    ) );

    if ( $issue->have_posts() ) {
        while ( $issue->have_posts() ) : $issue->the_post();

            // bisogno di poter usare parti di template qui dentro
           echo '<h2>Titolo numero = '.get_the_title().'</h2>';

        endwhile;
    }

    # reimposta il globale al post del primo loop
    $post = $preserve_post;
    setup_postdata( $post );
    // Attualmente restituisce il titolo del numero, non della pubblicazione
    echo '<h2>Titolo pubblicazione = '.get_the_title().'</h2>';

endwhile;
}
wp_reset_query();
13 dic 2013 12:10:43