If you need to have groups of repeating custom fields, you should use Types Field Tables. This tutorial shows how to do it.

Our goal is to display groups of repeating fields, which we can edit and display together. When we’re done, these fields will show at the bottom of the post-edit page. We’ll be able to display them using Views, or with Types PHP API.

child-form

The example we’ll use to demonstrate groups of repeating fields will be houses and rooms. Let’s say that we’ve create a custom post type called House. We’ll add groups of repeating fields for rooms. Each room will have a name, a description, a size and an image.

The setup steps includes:

  1. Create a container post-type for the repeating fields
  2. Create the fields group with the fields

Once we have that, we can edit the repeating fields. The final section of this tutorial will explain how to display the repeating fields.

Creating a Container Post Type for Repeating Fields

We need to create a container post, which will hold the repeating fields. In our example, we’ll create a Room custom post type.

Go to Types->Custom Types and Taxonomies and click on Add Custom Post Type. Fill in the information for the new custom post type (in our example, a Room) and scroll down to the Post Relationship section, at the bottom of the custom post type editing screen.

The critical step here is to make Rooms children of Houses. Click on the Edit button under parent and select House as the parent of the new Room type that you’re creating.

Parent settings for Room container post type

Remember to click Apply and save the post type. Now, you’ve got the Room post type, which will be the container for the custom fields.

Creating the Fields Groups and Custom Fields to Repeat

Next, we create a custom fields group, set it to display on the container post and add fields.

Go to Types->Custom Fields and click on Add a custom fields group. Give the group a name and start adding custom fields to it. Make sure to select that this group appears in the container custom post type (our Room).

Scroll to Where to display this group click on Edit next to the Post Types and select the container post (Room).

Select the container post for the group

Now add the fields to this group. Add any field you like. Just remember that these fields will appear one next to the other when you edit in them, so if you add a great number of fields, they will appear condensed.

These fields will show when editing the container post type (the Room) and also in the parent post type (the House).

Save the fields group and you’re done. Now, when you visit the parent post (the House), you’ll see controls for adding rooms, with their repeating custom fields.

Populating Repeating Custom Fields

Once the above setup is complete, whenever we edit a house, we will see a new section for creating rooms and entering their fields.

Scroll to the bottom of the post edit screen, to where it says Fields table. The Fields Table  section will become active after the first time you save the post (either as draft or published).

the fields table meta box for the parent page
Fields Table for creating groups of repeating custom fields

Click on Add New Room to create a new one. You will see inputs for all the fields that you added before.

Remember to save the fields. Unsaved rows will appear with yellow background. You can save individual rows, or all, using the Save All button.

Fields tables are flexible and let you sort entries, edit in full-details mode and delete items. You can read all about them in Editing Custom Fields of Child Post.

Displaying Groups of Repeating Fields

You can display repeating groups of custom fields using either Views (without any coding), or using Types PHP API and some of your own coding.

Using a View to Display Repeating Fields Groups

Remember that the fields belong to a Room container post type? We’re going to put this to good use when displaying using Views.

1. Create the View that loads the container posts (Rooms)

The View needs to load the container post items (rooms) and filter only the ones that belong to the parent (House) post. For this, click on Add another filter term and select:

Post Relationship – post is child of

Then, select Post where this View is inserted.

The result should be like in the View screenshot.

2. Add the fields to the View

The View will iterate through the container posts and will display whatever fields we add to it. Select your preferred layout and add fields.

3. Insert the View to the relevant Content Template

Now that we have a View that will display the repeating field group, we can add it to the Content Template of the post. Edit the Content Template, click on the V icon and add the View that you just created.

Using Types PHP API to Display Repeating Fields Groups

You are always welcome to use Types PHP API and build the display functionality yourself.

The procedure is similar to what we described with Views. We’ll query the child posts, which belong to the currently displayed page, iterate through them and echo the custom fields that we want to show.

For example, to show just the post title, we’ll use this code:

$child_posts = types_child_posts('room');
foreach ($child_posts as $child_post) {
  echo $child_post->post_title;
}

We will need to locate the template for the post-type that we need to modify and insert this code into it.