I have parent posts (custom post type hierarch=true) with ID 1, 2, 3, 4.
The parent posts with ID 2 and 4 have child pages.
I need to retrieve only posts 2 and 4 and all of their child pages.
Something like this
$argsch = array('orderby' => 'date','order' => 'DESC','post_type' => 'products', 'include' => '2, 4');
$childs = get_posts($argsch);
How can I edit this to return child pages of the included id’s?
3 Answers
You have two options:
-
Call get_posts multiple times: one for the parent pages using
post__in => array(2,4)
, and two for the child pages of each parent withpost_parent => 2
andpost_parent => 4
and finally merge all the results into a single array. -
Write directly your SQL query and use
$wpdb->get_results
. See this article for an example. In your case it would be something similar to the following (not tested) code:$query_sql = " SELECT * FROM $wpdb->posts WHERE post_type="products" AND (ID IN (2,4) OR post_parent IN (2,4)) ORDER BY post_date DESC "; $query_result = $wpdb->get_results($query_sql, OBJECT);