Home › Forums › Add-ons › Repeater Field › Automatically generate repeater fields
Hey guys.
well, firstly, title doesnt really describe my problem well enough so here is what i want to do (im new in the world of wordpress and stuff so bear with me).
So i have a “Number” field called Duration(which describes duration of a trip).
Then i have a repeater field to describe itinerary(detailed info for each day).
what i want to do:
1. Ideally, i want repeater field to generate as many rows as i sumbit into duration field. say i put 5 in the duration. and suddenly mysteriously there are five rows in the repeater field waiting for me to fill them out. i dont want users to be able to add more rows unless they increase number in duration field.
2. Second ideal situation would be that, Duration field is filled on its own according to how many rows there are in the repeater field. And i dont want duration field to be editable by the user, but they should be able to see what it shows.
well i hope u get at least general idea of what im trying to do here.
so any help or a better version my ideas would be appreciated ^^
P.S. along with some guidance ^^
Hi @jetro157,
I think the second one would be easier to achieve. If you only need to show the duration on the front-end, you can use the count() function on the repeater field. It should be something like this: count(get_field('repeater_field_name'))
.
If you need to show it on the backend, you can use acf/save_post to update the duration field using update_field(). Maybe something like this:
<?php
function my_acf_save_post( $post_id ) {
$itinerary = get_field('itinerary_field_name');
if ($itinerary){
$duration = count($itinerary);
update_field('duration_field_name', $duration, $post_id);
}
}
// run after ACF saves the $_POST['acf'] data
add_action('acf/save_post', 'my_acf_save_post', 20);
?>
I hope this helps.
this worked perfectly! thanks… and one more question if i may.
what if i have the repeater field in flexible content?
Hi @jetro157,
I think you need to get the repeater fields first from the flexible field. Maybe something like this:
<?php
function my_acf_save_post( $post_id ) {
if( have_rows('flexible_content_field_name', $post_id) ):
$current_duration = intval(get_field('duration_field_name'));
while ( have_rows('flexible_content_field_name', $post_id) ) : the_row();
if( get_row_layout() == 'itinerary_layout' ):
$itinerary = get_sub_field('itinerary_field_name');
if ($itinerary){
$duration = count($itinerary);
$current_duration = $current_duration + $duration;
}
endif;
endwhile;
update_field('duration_field_name', $current_duration, $post_id);
endif;
}
// run after ACF saves the $_POST['acf'] data
add_action('acf/save_post', 'my_acf_save_post', 20);
?>
Keep in mind that I haven’t tested it yet. Please fix the error message as needed.
Hope this helps.
The topic ‘Automatically generate repeater fields’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.