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.
- Making Custom Fields Repeatable
- How Types Stores Repeater Fields
- Displaying Repeating Fields
- Index: Displaying a single value for a repeating field
- Groups of Repeating Fields
1. Making Custom Fields Repeatable
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.
2. 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 check-boxes repeating. Because check-boxes are boolean, it makes no sense to set both true and false options for the same key.
3. 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.
[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.
4. 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
[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.
5. Groups of Repeating Fields
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.