Repeating fields are custom fields that have more than one value. Types and Views let you create repeating custom fields.
Repeating (repeater) fields are useful when you need to include several instances of the same field. For example, people might have several phone numbers. You can use repeating fields to allow adding different phone numbers to a single ‘person’.
Then, you can display these fields using Types API functions or using Views.
When you edit a custom fields group, you’ll see a checkbox that says:
“This fields can appear multiple times when editing”
When you enable this checkbox, the content editing screen will include ‘Add another field’ buttons for those fields.
Here is how it looks:
You can also delete fields that you’ve added.
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:
You might also notice that you cannot make check-boxes repeating. Because check-boxes are boolean, it makes no sense to set both true and false options for the same key.
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.
[types field="my-number" separator=","][/types]
1, 2, 4, 6
<ul><li>[types field="my-number" separator="</li><li>"][/types]</li></ul>
will output as an unordered list
<ol><li>[types field="my-number" separator="</li><li>"][/types]</li></ol>
will output as an ordered list
If you want to fine-tune the output for repeater fields, you can use the Views wpv-for-each shortcode. For example, to output the items in an unordered-list, you can do this:
<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.
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
[types field="mylist" index="1"][/types]
Would return the second value for the repeater field, mylist.
You can change the order of the repeater field values directly from the post edit screen.
Types repeating fields are intended for single fields that you want to duplicate.
Sometimes, it makes sense to group fields together and edit them together. Types makes this possible using Field Tables. You’ll define a container custom post that groups the fields together, and then you can edit the fields of these child items, when you’re editing the post.
Visit the complete tutorial on setting up and displaying groups of repeating fields.