Querying and Displaying Child Posts

Now that you’ve defined parent / child relationship, you’re probably looking for a way to display those children. I’ll show you how to do it using a View.

Let’s use our house / rooms example. When we display a house, we’d like to show its rooms.

Displaying child posts using Views

We have two cases to consider:

  • When displaying a single house, display its rooms.
  • When listing houses, display rooms for each house.

In the first case, when we display a single house, we’ll need to display child posts (rooms) of the current page (house). In the second case, when we display a list of houses, we’ll display child posts of the parent set by the View.

Create a View and choose to load the child items. In our case, Rooms.

Then, add a filter. Choose ‘Post relationship – post is child of’:

Filter by post relationship

Filter by post relationship

To use this View in a single House page, you should choose ‘post where this View is inserted’. It means that you’ll get Rooms that belong to the displayed House.

And, if this View goes inside another View which lists Houses, you should choose ‘post set by parent View’. Then, you’ll get the Rooms of the House in the external View. Once you’ve created this View for rooms, you can insert it into the Houses View. The most convenient way to do this is to use a Content Template for each House and include the Rooms View inside it.

There are other two options in that filter: post with ID set by a shortcode attribute or with ID set by an URL parameter. They will let you reuse the same View but filtering by different post relationships. Take a look at the documentation about passing arguments to Views.

Now, we’re practically done. This View will load the Rooms that we need and all that’s left to do is to actually display them.

Insert 3 column grid

Insert 3 column grid

Listing Room Content Template

Listing Room Content Template

Use the Layout section to control the output. When you insert fields, you’ll see just Room fields.

And, this is how it looks:

Rooms displayed for a House

When we view a House, we can see all its rooms.

Displaying child posts using Types PHP functions

Alternatively, you can load child posts using Types types_child_posts API function.

types_child_posts($post_type, $args)

  • $post_type – a string with the name of the child items to load. Since a parent may have children of different types, this argument is required.
  • $arg – optional, with additional query arguments.

For example, to load ‘room’ child items:

$child_posts = types_child_posts(‘room’);
foreach ($child_posts as $child_post) {
  echo $child_post->post_title;
  echo $child_post->fields['description'];
}

If you need more control of the returned posts you can create a query directly. This example returns all rooms that belong to the current property and will sort them by the description field.

$childargs = array(
'post_type' => 'room',
'numberposts' => -1,
'meta_key' => 'wpcf-description',
'orderby' => 'meta_value',
'order' => 'ASC',
'meta_query' => array(array('key' => '_wpcf_belongs_property_id', 'value' => get_the_ID()))
);
$child_posts = get_posts($childargs);

More reading on this topic