Skip Navigation

[Resolved] Display repeater field group with fields in PHP

This thread is resolved. Here is a description of the problem and solution.

Problem:
How to display repeatable field groups using PHP?

Solution:
Each group of repeatable fields is stored in a child post, so if a post has 3 groups of repeatable fields then that post will be connected to 3 child posts which each store the field values.

You use the relationships API, specifically the function toolset_get_related_posts to get the child posts, and you then iterate over those to get and output the field values as required.

Relevant Documentation:
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_posts

This support ticket is created 5 years, 2 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+00:00)

This topic contains 3 replies, has 2 voices.

Last updated by MennoB5853 5 years, 2 months ago.

Assisted by: Nigel.

Author
Posts
#1328067

Hi Toolset support,

I'm trying to display fields from a repeater field group with PHP but i don't know how to do this.. Can you share a small example?

https://toolset.com/forums/topic/display-repeating-fields-group-using-php/

Thanks,
Menno

#1328071

Oh this might work :):

<?php 
$child_posts = toolset_get_related_posts( get_the_ID(), 'employee-experience', array( 'query_by_role' => 'parent', 'return' => 'post_object' ) );
foreach ($child_posts as $child_post) 
{ ?>
          
<div class="car-listing">
    <h5><?php echo $child_post->post_title; ?></h5>
    <?php echo types_render_field( "work-experience-function", array( "id"=> "$child_post->ID")); ?>
</div>
<?php } ?>
#1328149

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi Menno

That's right, each group of repeatable fields is stored in a child post, so if a post has 3 groups of repeatable fields then that post will be connected to 3 child posts which each store the field values.

So you use the relationships API, specifically the function toolset_get_related_posts to get the child posts, and you then iterate over those to get and output the field values as required.

Just a note of caution, the solution in your first link is from an older implementation of the toolset_get_related_posts function, you should review the documentation for the current version (which your code sample uses).

https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_posts

The "relationship slug" for repeatable field groups will be the slug of the repeatable field group.

#1329191

Thanks Nigel. My issue is resolved now.