You need a loop for this to handle the condition:
/** list for course and childless department pages */
$course_and_childless_department_pages = array();
/** get department pages */
$department_pages = get_children( array(
'post_parent' => $post->ID,
'post_type' => 'page',
'post_status' => 'publish',
) );
foreach ( $department_pages as $department_page ) {
/** get course pages (for department page) */
$course_pages_for_department_page = get_children( array(
'post_parent' => $department_page->ID,
'post_type' => 'page',
'post_status' => 'publish',
) );
if ( empty( $course_pages_for_department_page ) ) {
/** add empty department page to list */
$course_and_childless_department_pages[] = $department_page;
} else {
/** add course pages to list */
$course_and_childless_department_pages = array_merge( $course_and_childless_department_pages, $course_pages_for_department_page );
}
}
return $course_and_childless_department_pages;
You wrote that you are on the Departments page, so the value of $post
is the Departments page.
$course_and_childless_department_pages
array will contain all the pages in WP_Post
format.