web-dev-qa-db-fra.com

Boucle WP_Query dans la boucle WP_Query

J'essaie d'utiliser une boucle WP_Query personnalisée dans une autre pour afficher les données de la boucle interne indépendamment de la boucle principale. J'ai trouvé cette solution mais je ne comprends pas vraiment comment l'utiliser. Voici le code, ça marche mais la boucle principale génère des posts en double.

<?php
// define the main query
$main_args = array(
    'post_type' => 'page',
    'post_parent' => '10',
);
// execute the main query
$the_main_loop = new WP_Query($main_args);
// go main query
if($the_main_loop->have_posts()) : while($the_main_loop->have_posts()) : $the_main_loop->the_post(); 
?> 
    <p>This is the content from the main loop</p>

    <?php
    // define the inner query
    $inner_args = array(
        'post_type' => 'page',
        'post_parent' => '20',
    );
    // execute the inner query
    $the_inner_loop = new WP_Query($inner_args);
    // go inner query
    if($the_inner_loop->have_posts()) : while($the_inner_loop->have_posts()) : $the_inner_loop->the_post(); 
    ?> 
        <p>This is the content from the inner loop</p>
    <?php 
    // end the inner loop, no reset
    endwhile; endif;
    ?> 

    <p>This is another content from the main loop</p>

<?php 
// end the main loop
endwhile; endif; wp_reset_postdata();
?>

Merci!

1
jjj

On ne sait toujours pas ce que vous voulez réellement faire, mais selon ceci:

cela fonctionne mais la boucle principale génère des posts en double

Je peux vous dire que c'est la sortie attendue.

Votre boucle "principale" interroge et affiche toutes les pages ayant un ID de page parent de 10. Disons que 10 pages correspondent à votre requête "principale", cela signifie que votre boucle parcourra 10 cycles si vous voulez l'appeler ainsi. Pensez à la boucle en tant que boucle foreach car elle est essentiellement juste cela ( mais pas exactement la même chose ).

Maintenant, à chaque cycle, vous ajoutez une autre requête qui interroge les pages avec un ID parent de 20. Disons qu'il y en a aussi 10. Ainsi, chaque cycle de la boucle "principale" affichera 10 posts avec le post parent de 20, 10 fois car il y a 10 pages avec un parent de 10.

Très basique, vous avez:

// First cycle 
the_post(); // 1st page with parent 10
    // custom query which will display pages from parent 20
// End of first cycle and start of second cycle
the_post();  // 2nd page with parent 10
    // custom query which will display pages from parent 20
// End of second cycle and start of third cycle
the_post();  // 3rd page with parent 10
    // custom query which will display pages from parent 20
etc etc

Vous devriez prendre votre deuxième requête en dehors de la "principale" pour les séparer

$main_args = [
    'post_type' => 'page',
    'post_parent' => '10',
];
// execute the main query
$the_main_loop = new WP_Query($main_args);
// go main query
if($the_main_loop->have_posts()) { 
    while($the_main_loop->have_posts()) { 
    $the_main_loop->the_post(); 

        // Display your loop content

    } // endwhile
    wp_reset_postdata(); // VERY VERY IMPORTANT
}

// define the inner query
$inner_args = [
    'post_type' => 'page',
    'post_parent' => '20',
];
// execute the inner query
$the_inner_loop = new WP_Query($inner_args);
// go inner query
if($the_inner_loop->have_posts()) {
    while($the_inner_loop->have_posts()) {
    $the_inner_loop->the_post();

        // Display your loop content

    } // endwhile
    wp_reset_postdata(); // VERY VERY IMPORTANT
}
3
Pieter Goosen