I have an array of IDs stored for custom post type "service". Some of these are parent IDs and some of these are child IDs.
So "service" post type here is hierarchical.
Now from this array of IDs, I want to list pages in the format so that:
They are sorted by post title, ascending order.
They are listed in parent > child pattern like this:
Service 1 A Child B Child Service 2 X Child Y Child Service 3
Also as the array contains any IDs, if there is child ID in the array, but parent ID is not then show the parent post automatically.
So far below is the code I have implemented:
$args = array(
'post_type' => array( 'service' ),
'posts_per_page' => '-1',
'orderby' => 'title',
'order' => 'ASC',
'post__in' => $service_ids_array,
);
$all_services = new WP_Query( $args );
But, this just gets all the services from the array of IDs sorted by title.
Here is a logic I am trying to implement, but if there is a better solution, I am happy to use that.
So my logic was, to get all the posts from array that are on root level. So parent ID = 0.
After that I run a loop of these IDs and in the loop I check if their child IDs exist in the $service_ids_array
or not. And show if they exist.
But what if there are only child IDs in the array? In that case this logic will not succeed.