I have an hierarchical custom post type called Geographies, wherein each Geography is assigned to a custom taxonomy (geographytype): Area, Country, Region or City. The hierarchy is set as follows:

Area ¬
     Country ¬

As an example: The city of Boston is a child of United States, which in turn is a child of Americas.

I want to be able to get children and/or grandchildren of a Geography, and specify its geographytype - for example, if I'm viewing Americas, I want to get all the cities, including Boston.

However, it seems like I can only do one or the other. If I use get_pages(), I can get all children and grandchildren by specifying the parent parameter, but I can't specify a geographytype, so I just get everything.

Conversely, if I use WP_Query() I can specify my geographytype using the tax_query parameter, but it won't give me grandchildren; I can only get direct children using the post_parent parameter.

I want to be able to do both: get children and grandchildren, with a specified custom taxonomy.

1 Answer 1


I was able to achieve this by, funnily enough, using with get_pages() and WP_Query(). I wanted a reusable function that would return a WP_Query array that I could loop through, so I came up with this:

function returnChildGeographies($geoID, $geoType, $grandChildren = false) {

    $args = array(
        'post_type'         => 'geographies',
        'posts_per_page'    => -1,
        'orderby'           => 'post_title',
        'order'             => 'ASC',
        'post_parent'       => $geoID,
        'tax_query'         => array(
            'taxonomy' => 'geographytype',
            'field' => 'slug',
            'terms' => $geoType

    if ($grandChildren) {
        $getPagesArgs = array(
            'sort_order' => 'ASC',
            'sort_column' => 'post_title',
            'hierarchical' => 0,
            'child_of' => $geoID,
            'post_type' => 'geographies'

        $pagesArray = get_pages($getPagesArgs);
        $grandChildArray = array();

        foreach ($pagesArray as $grandchild) {
            $grandChildArray[] = $grandchild->ID;

        $args['post__in'] = $grandChildArray;

    $childArray = new WP_Query($args);

    return $childArray;

As an example usage: if I'm viewing the Americas area and I want to get all of the cities (which are which are grandchildren of the area), I call:

$regionQuery = returnChildGeographies($post->ID, 'city', true);

If I only want direct children:

$countryQuery = returnChildGeographies($post->ID, 'country', false);

Based on that last parameter, the function will unset post_parent and set post__in, which is an array of the post IDs I'm looking for.

The function could probably be neatened up quite a bit, but the method stands: combining get_pages() and WP_Query() seemed like the best way of getting grandchildren (or great-grandchildren, or great-great-grandchildren) and using a custom taxonomy query.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.