Restablecimiento de datos de publicaciones al bucle anterior en bucles anidados

19

Estoy intentando usar bucles anidados con el complemento de publicaciones a publicaciones. Ambos bucles funcionan, pero el problema surge después del segundo bucle anidado (problema $). Quiero volver a acceder al $ bucle de publicación, pero los datos siguen siendo los datos de $ problem.

wp_reset_query() se restablecerá de nuevo al bucle principal en single.php que no quiero.

Podría usar get_posts() en lugar de WP_Query nuevo, pero quiero poder usar get_template_part() .

¿Cómo puedo restablecer mis datos al bucle de publicación, de modo que el segundo 'Título de publicación' devuelva la publicación, no el problema, el título?

Aquí está mi código dentro de 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>Publication title = '.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();

            // need to be able to use template parts in here
            echo '<h2>Issue title = '.get_the_title().'</h2>';

        endwhile;
    }

    // This currently returns the issue title, not the publication title
    echo '<h2>Publication title = '.get_the_title().'</h2>';

endwhile;
}
    
pregunta kdev 13.12.2013 - 10:33

2 respuestas

20

Voy a responder esto yo mismo, pero fue el muy inteligente @simonwheatley de Code for the People el que resolvió esto por mí.

En lugar de usar wp_reset_postdata() o wp_reset_query() , puedes usar lo siguiente:

$publication->reset_postdata();

Donde $ publicación es su objeto de consulta.

El código de trabajo ahora parece:

$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>Publication title = '.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();

            // need to be able to use template parts in here
            echo '<h2>Issue title = '.get_the_title().'</h2>';

        endwhile; $publication->reset_postdata();
    }

    echo '<h2>Publication title = '.get_the_title().'</h2>';

endwhile;
}
    
respondido por el kdev 13.12.2013 - 11:04
5

En primer lugar, creo que es posible usar get_posts() en combinación con setup_postdata() . Con estos, puede usar las etiquetas de plantilla como en un bucle normal de WordPress.

Pero también puedes usar esta función en tus bucles anidados:

# make sure $post is the global in your scope (which should be the case in single.php)
global $post;
if ( $publication->have_posts() ) {
while ( $publication->have_posts() ) : $publication->the_post();
    echo '<h2>Publication title = '.get_the_title().'</h2>';
    $pub_id = get_the_ID();

    # preserve the current post in the higher loop
    $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();

            // need to be able to use template parts in here
           echo '<h2>Issue title = '.get_the_title().'</h2>';

        endwhile;
    }

    # set the global back to your first loop post
    $post = $preserve_post;
    setup_postdata( $post );
    // This currently returns the issue title, not the publication title
    echo '<h2>Publication title = '.get_the_title().'</h2>';

endwhile;
}
wp_reset_query();
    
respondido por el David 13.12.2013 - 11:10

Lea otras preguntas en las etiquetas