Is there any way to specify the number of repeater fields on the front-end?
I’m able to successfully display a repeater field on the front-end allowing users to enter values but I don’t want them to be able to enter as many as they want or as few as they want. I want to specify exactly how many.
Is this possible?
Hi @moonshdw8
You can limit the number of rows to be added to the repeater by setting the maximum rows option from the field group edit page.
Hope this helps.
Thanks a lot for the quick reply, but I want to be able to do this programatically.
In this case, if someone has, let’s say 15 items. I want to display 15 repeater fields.
Or 5 items, 5 repeater fields. etc.
Thanks again.
Incidentally, this is my existing code
acf_form(array(
'fields' => array('field_5d366cc2c5656'),
));
For anyone else with this issue, I resolved it this way:
I used the acf/load_value as outllined here: https://gist.github.com/theJasonJones/7061b8020e43a114756214e20c4d0ed9
Fantastic Help!
Instead of adding each item like they do int he tutorial, I had a global variable based on how many items I needed, then I looped through the “$value[] = array(…” creating as many repeater items as I need.
Here’s my resultant code:
// Sets the number of Repeater Confirm Ring Size items.
add_filter('acf/load_value/key=field_5d366cc2c5656', 'afc_load_my_repeater_value', 10, 3);
function afc_load_my_repeater_value($value, $post_id, $field) {
global $ring_count;
//Optional: Check for post_status otherwise published values will be changed.
if ( get_post_status( $post_id ) === 'wc-processing' or get_post_status( $post_id ) === 'wc-pending') {
//Optional: Check for post_type.
if( get_post_type( $post_id ) == 'shop_order' ){
if ($ring_count > 0){
$value = array();
// Add field key for the field you would to put a default value (text field in this case)
for ($i = 0; $i < $ring_count; $i++) {
$value[] = array(
'field_5d366d10c5657' => '5'
);
}
}
}
}
return $value;
}