Repeating fields are custom post fields that can have more than one value. Types and Views let you create and display repeating post fields.

Repeating fields are useful when you need to include several instances of the same field. For example, a “house” might have several images.

Then, you can display these repeating fields using Types API functions or using Views.

Making Repeating Post Fields

When you edit a post field group, in the post field section you can see the Single or repeating field setting.

Making a field to repeat

When you select Allow multiple-instances of this field, the post editing screen will include buttons to add more values for those fields.

Editing with repeating fields

Adding Multiple Images at Once

If you are adding media files like photos or videos you can add more than one media file at the time. There is no need to click Add new button for each photo. You just need to do the following:

  1. Click Select image button.

Select image
Select image

  1. In a dialog box that will open up select as many images as you like and click Use this as field value button.

Selecting multiple images
Selecting multiple images

You will see that all of the selected images have been added to your post as multiple fields.

Multiple fields
Multiple fields

How Types Stores Repeater Fields

Types stores repeating fields as individual post-meta in the database. This means that when you add another field, a new post-meta is created with the same key, but a different value.

This implementation dictates the following:

  • Repeating fields can have same values – meaning, you can add the same ’email’ twice.
  • You can reorder repeating fields using drag buttons. WordPress loads them as an array. It’s also possible to sort this array when displaying.
  • Repeating fields are not sensitive to Types implementation – since they use the standard WordPress storage, either Types or any other plugin can access them. This is the main reason behind our choice to implement them as individual items.
  • You can filter by repeating fields – because they are stored as regular post-meta, Views filters run just fine on repeating fields.

You might also notice that you cannot make checkboxes repeating. Because checkboxes are boolean, it makes no sense to set both true and false options for the same key.

Displaying Repeating Fields

Types API will load all values for repeating fields and display them one after the other. You can specify the separator that goes between items.

This code:

Specifying the separator
[types field="my-number" separator=","][/types]

will output

1, 2, 4, 6

This code:

Unordered list
<ul>
<li>[types field="my-number" separator="</li><li>"][/types]</li>
</ul>

will output as an unordered list

  • 1
  • 2
  • 4
  • 6

This code:

Ordered list
<ol>
<li>[types field="my-number" separator="</li><li>"][/types]</li>
</ol>

will output as an ordered list

  1. 1
  2. 2
  3. 4
  4. 6

If you want to fine-tune the output for repeating fields, you can use the Views wpv-for-each shortcode. For example, to output the items in an unordered-list, you can do this:

Output the items in an unordered list
<ul>
[wpv-for-each field="wpcf-test-upload-repeater"]
<li>[types field="test-upload-repeater"][/types]</li>
[/wpv-for-each]
</ul>

wpv-for-each loops through all instances for the specified field and displays the content inside the block. This way, you can easily include markup before and after each field.

Index: Displaying a single value for a repeating field

The index parameter will return a single value from a repeating field.
This uses zero-based numbering, so the first element has an index=”0″ and the third, an index=”2″ etc

Repeating field index parameter
[types field="mylist" index="1"][/types]

Would return the second value for the repeating field, mylist.

You can change the order of the repeating field values directly from the post edit screen.

Displaying Repeating Fields using PHP

If you are using only the Types plugin, you can display repeating fields in your templates using custom PHP code. Learn more about this topic on the page about displaying repeating fields using PHP.