I'm working on a personal project which includes multiple custom fields (created via ACF). A few of them are repeater types.
I want to to display 3 arrays/blocks of those repeater subfield values. For better understanding, I have this repeater field: open_workshops, and this field includes subfields: date, location, partnerships.
I want simply to show all values from those subfields stored in DB. Something like:
Open Workshops:
Date: Jan 2017, Feb 2017...Dec 2019 etc
Location: New York, Warsaw...
Partnerships: EY, Google..
What issues I've noticed - first of all, because of field type (repeater) it's damn hard to find those values in the DB. Because its not a single field but ACF replicates their names, so instead of looking for single field: open_workshops_date i need somehow to find: open_workshops_0_date, open_workshops_1_date etc.
My initial code was:
if( have_rows('open_workshops') ):
while ( have_rows('open_workshops') ) : the_row();
$sub_value_1 = get_sub_field('date');
$sub_value_2 = get_sub_field('location');
$sub_value_3 = get_sub_field('partnerships');
echo '$sub_value_1';
echo '$sub_value_2';
echo '$sub_value_3';
endwhile;
else :
// no rows found
endif;
I've tried as well the suggestion from this post: Retrieving all data from repeater fields
but it shows nothing.
I'm working on a personal project which includes multiple custom fields (created via ACF). A few of them are repeater types.
I want to to display 3 arrays/blocks of those repeater subfield values. For better understanding, I have this repeater field: open_workshops, and this field includes subfields: date, location, partnerships.
I want simply to show all values from those subfields stored in DB. Something like:
Open Workshops:
Date: Jan 2017, Feb 2017...Dec 2019 etc
Location: New York, Warsaw...
Partnerships: EY, Google..
What issues I've noticed - first of all, because of field type (repeater) it's damn hard to find those values in the DB. Because its not a single field but ACF replicates their names, so instead of looking for single field: open_workshops_date i need somehow to find: open_workshops_0_date, open_workshops_1_date etc.
My initial code was:
if( have_rows('open_workshops') ):
while ( have_rows('open_workshops') ) : the_row();
$sub_value_1 = get_sub_field('date');
$sub_value_2 = get_sub_field('location');
$sub_value_3 = get_sub_field('partnerships');
echo '$sub_value_1';
echo '$sub_value_2';
echo '$sub_value_3';
endwhile;
else :
// no rows found
endif;
I've tried as well the suggestion from this post: Retrieving all data from repeater fields
but it shows nothing.
Share Improve this question edited Dec 19, 2019 at 14:46 RiddleMeThis 3,8078 gold badges22 silver badges30 bronze badges asked Dec 15, 2019 at 20:08 Paweł SkabaPaweł Skaba 1502 silver badges12 bronze badges3 Answers
Reset to default 1 +50I don't think ACF has a built-in function to do what you want. You can use get_field to retrieve a value from any post, but it requires a post ID if you want the value from anything other than the current post.
So instead, we can query posts using WP_Query and pass the meta key of our custom field.
Here is an example.
$args = array(
'post_type' => 'page', // Add your post type here
'meta_key' => 'test_repeater' // Add your repeater name here
);
$the_query = new WP_Query($args);
if ($the_query->have_posts()):
while ($the_query->have_posts()) : $the_query->the_post();
if(have_rows('test_repeater')): // Add your repeater here
while (have_rows('test_repeater')) : the_row(); // Add your repeater here
// display your sub fields
the_sub_field('sub_field_one');
the_sub_field('sub_field_two');
endwhile;
else :
// no rows found
endif;
endwhile;
endif;
I added the above code to a page template, it will spit out the values of my test_repeater
for any page
that has those custom fields filled out.
Tested and works.
Have you tried passing post id? Please see below:
if( have_rows('parent_field', $post_id) ):
while ( have_rows('parent_field', $post_id) ) : the_row();
$sub_value = get_sub_field('sub_field');
// Do something...
endwhile;
else :
// no rows found
endif;
the Repeater field of ACF follows a simple method, this set of code will help you.
if( get_field('open_workshops', get_the_ID()) ):
$counter = 0;
while( the_repeater_field('open_workshops', get_the_ID())):
//this sets up the counter starting at 0
echo get_field('open_workshops_'.$counter.'_date', get_the_ID());
echo get_field('open_workshops_'.$counter.'_location', get_the_ID());
echo get_field('open_workshops_'.$counter.'_partnerships', get_the_ID());
$counter++; // add one per row
endwhile;
endif;