1

I'm trying to use a custom WP_Query loop within another one to display data from the inner loop independently from the main loop. I found this solution but don't really understand how to use it. Here is the code, it works but the main loop generate duplicate posts.

<?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();
?>

Thanks!

5
  • In order to get a good answer you should actually ask a question. What are you trying to accomplish that would require nesting queries?
    – jdm2112
    Dec 29, 2015 at 15:18
  • What do you mean by "use it"? Its code. It gets gets written into a file. But you don't explain what you want to do.
    – s_ha_dum
    Dec 29, 2015 at 15:26
  • ok, sorry for the bad asking, i'm trying to have data from the inner loop independently from the main loop.
    – jjj
    Dec 29, 2015 at 15:27
  • Your edit still says nothing. Is the code not working, do you have issues with the code, are you getting errors, what are those errors. Dec 29, 2015 at 16:09
  • Question just edited, sorry for the lazy feeling.
    – jjj
    Dec 29, 2015 at 17:01

1 Answer 1

4

It is still totally unclear what you actually want to do, but according to this:

it works but the main loop generate duplicate posts

I can tell you that it is expected output.

Your "main" loop queries and displays all pages which has a parent page ID of 10. Lets say there are 10 pages that matches your "main"query, this will mean your loop will run through 10 cycles if you want to call it that. Think of the loop as a foreach loop as it is essentially just that (but not exactly the same).

Now, within each and every cycle, you are adding another query which queries pages wit a parent ID of 20. Lets say there are also 10. So each and every cycle of the "main" loop will output 10 posts with post parent of 20, 10 times because there are 10 pages with a parent of 10.

Very basic, you have:

// 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

You should take your second query outside the "main" one to separate them

$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
}
2
  • Thanks for your explanation and for your time, it help me a lot to understand cycles of a loop. My problem is more about to put a loop queries inside another one but I realize that my example and my question are not clear, i'm very sorry (again) about that. I'll take care to do it better, next time on StackExchange.
    – jjj
    Dec 29, 2015 at 17:44
  • 1
    My pleasure. You should try to get an exact use-case for exact answers. I have done a post or two about using a query in another one, something like related posts if I can remember correctly, but frankly, I do not know when and where. Feel free to look through my answers ;-) Dec 29, 2015 at 17:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.