I'm using WP_Query
to get all pages without subpages. For each page I'm using again WP_Query
to get all child pages of the current page inside the first loop.
This all works fine and I'm getting the expected result.
But when now using a third WP_Query
on one of the sub pages, the second level loop breaks.
I already tried the functions wp_reset_query()
and wp_reset_postdata()
but they didn't do the trick.
Also I tried to save the global $wp_query
and global $post
and reset them after the loop, but this didn't help too.
How do I implement nested queries in WP?
Edit As requested (and how it would have been correct) here is my code. I also realized that my problem does not apply only to the third, but also to the second loop.
$all_pages = new WP_Query(array('post_type' => 'page', 'post_status' => 'publish', 'post_parent' => 0));
while($all_pages->have_posts()) {
$all_pages->the_post();
include(get_page_template());
}
wp_reset_postdata();
In the page templates I'm using the following code to get child pages:
// child page content rendered here, depending on template
if($post->post_parent == 0) {
$child_pages = new WP_Query(array('post_type' => 'page', 'post_status' => 'publish', 'page_parent' => $post->post_parent));
while($child_pages->have_posts()) {
$child_pages->the_post();
include(get_page_template());
}
wp_reset_postdata();
}
I can execute WP_Queries on the child pages. But when then using the_post();
the $child_page
loop stops executing.
get_template_part
notinclude
to pull in template files