Return parent post with its children using WP_Query?
In some cases it might be useful to use multiple post page parameters in your WP_Query
object. In my case, I would like to display children of a parent page including the parent page itself.
Visualization of what I want to achieve. Imagine the following pages hierarchically sorted as following:
- page A
- page B
- Child page A
- Child page B
- Child page C
- page C
The bold list items are the posts/pages I want to retrieve.
My first thoughts go out using these two parameters for WP_Query
:
$args = array(
'post_id' = $parent-ID,
'post_parent' = $parent-ID,
);
Unfortunately, here it will only use one parameter. With the $args
above (correct me if I'm wrong) it will output all children posts of the parent post and not the parent post itself as well.
This problem might be solved by gathering all posts needed and putting them in parameter post__in
like so:
$args = array(
'post__in' = $children_and_parent_ids,
);
However there is wp_list_pages()
allowing you to include
a post(s) and specify the post where you want to include the children of (child_of
). Why is this not possible with WP_Query
?
An example of what I'm trying to achieve using wp_list_pages()
:
wp_list_pages(array(
'include' = $parent-ID,
'child_of' = $parent-ID,
));
Have a look at the documentation of WP_Query
.
Topic wp-list-pages wp-query Wordpress
Category Web