Displaying Fields of Child Posts

Once you’ve defined a post type relationship and added content to your fields table, you’ll be ready to display it. There are two ways to do this:

  • Using Views
  • Using the Types API

Let’s take a look at both.

Using Views

We’ve added a filter to Views so that you can easily display content. Create a new View and then in the “Filter” section click on “Add New Filter.”

From the dropdown menu select “Post Type is a child of”.

Selecting a parent for a child view

There are three conditions that you can choose from here:

  • Post Type where this View is inserted – this will display posts that are the children of the post type where the View is inserted. I.e. if it’s inserted into a Page the results will be post types that are the children of that page
  • Post set by the Parent View – if the View is inserted into another View, this will output based on the parent View
  • Specific – use to output the child posts of a specific piece of content.

You can then set up the rest of your View as usual. To see a practical example of using a View to output your fields table content, check out this tutorial for displaying rooms belonging to a Real Estate listing.

Using the Types API

If you’re a developer you may want to use the Types API to display your content. We’ve added an API call to embedded/types.php

Here’s what it looks like:

types_child_posts($post_type, $args)

This queries child post types and then fetches the meta fields using Types. It keeps them in this property:

$child_post->fields (array)

In your template files you can use it like this inside the loop:

$child_posts = types_child_posts('show');
foreach ($child_posts as $child_post) {
echo $child_post->post_title; echo $child_post->fields['location'];
}

You can read more about the Types API here.