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.
When you edit a post field group, in the post field section you can see the Single or repeating field setting.
When you select Allow multiple-instances of this field, the post editing screen will include buttons to add more values for those 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.
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 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:
<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 repeating field, mylist.
You can change the order of the repeating field values directly from the post edit screen.
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.