0

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.

1
+50

I 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.

| improve this answer | |
0

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;
| improve this answer | |
  • the thing is, I dont know the exact post_id. ACF stores those subfields in the db, obviously each CPT post has subfield value attached to themselves. But I dont want to display those values in CPT post. I want to show all those values in a separate/independent page (lets call it summary page). The page has nothing to do with that CPT. – Paweł Skaba Dec 18 '19 at 13:40
-2

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;
| improve this answer | |
  • Copying my previous comment (up) - the thing is, I dont know the exact post_id. ACF stores those subfields in the db, obviously each CPT post has subfield value attached to themselves. But I dont want to display those values in CPT post. I want to show all those values in a separate/independent page (lets call it summary page). The page has nothing to do with that CPT. – Paweł Skaba Dec 18 '19 at 13:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.