Repeating fields are post fields that have more than one value. Types and Views let you create repeating post 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.
- Making Post Fields Repeatable
- How Types Stores Repeater Fields
- Displaying Repeating Fields
- Index: Displaying a single value for a repeating field
- Groups of Repeating Fields
Making Post Fields Repeatable
When you edit a post field group, in post field section you can see a setting Single or repeating field?.
When you select Allow multiple-instances of this field, the content edit screen will include buttons to add more values for those fields.
Here is how it looks:
You can also delete fields that you have already added.
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.
[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.
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.
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 Fields of Child Posts. 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.