Skip Navigation

[Resolved] Calling repeating field group with multiple field values in a post loop

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

Problem: I would like to access the fields in a RFG within the loop of the parent post type.

Solution: You can use a View of RFGs with a post relationship filter to loop over RFGs, or you can use the post relationships API to query related posts. Then use the Types Render Field API to render individual fields from each RFG iteration.

0% of people find this useful.

This support ticket is created 3 years, 11 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
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 4 replies, has 3 voices.

Last updated by stephaneb-7 3 years, 11 months ago.

Assisted by: Christian Cox.

Author
Posts
#1954759

I think I've looked at every post you have on this topic and still don't understand the relationships.
I can call a single field value in a post loop into my template just fine, but I don't understand the array structure of a repeating field group.

I want all the custom field values from the repeating field "positions-and-names" while in the loop of custom post type "school".

CPT: school
Repeating field group slug: positions-and-names
Repeating field -> Field 1 slug: position_of_contact
Repeating field -> Field 2 slug: name

This can't be right:
types_render_field( "positions-and-names", array( "position_of_contact"=> "", "name" => "", "separator" => "<br>"));

#1954765

Hi, there's no simple way to use the Types Render Field API to display fields from all the RFGs associated with a post. if you want to loop over all the repeatable field groups (RFGs) associated with a post and display some fields from those RFGs, the most effective way is to use a View of that RFG and include the fields in the loop of that View. Usually you'll want to include a post relationship or repeatable field groups owner Query Filter, to limit the results of that View to only the RFGs associated with one particular post. Otherwise, all RFGs from all posts will be returned.

If you're looking for a PHP API to render a View, you can find information about the Views API available here:
https://toolset.com/documentation/programmer-reference/views-api/

Within the loop of that View, you could use the Types Render Field API to output one field from one instance of the RFG, but the simpler way is to insert that field in the loop, either using a Single Field block in the block editor, or using the Types field shortcode in the legacy editor.

Let me know if you have questions about this.

#1954783

My issue is resolved now. Thank you!
It seems a bit convoluted thinking of a field as a "post" instead of meta - that was the problem. I'll put my solution here in case it helps someone:

//in the loop for the post type
$fieldvalues = toolset_get_related_posts( get_the_ID(), "group-field-slug", array( 'query_by_role' => 'parent', 'return' => 'post_object'));
foreach ($fieldvalues as $fieldvalue) {

$partone = types_render_field('field-one-slug', array('post_id' => $fieldvalue->ID));
$parttwo = types_render_field('field-two-slug', array('post_id' => $fieldvalue->ID));

echo $partone.": ".$parttwo;
}

#1954787

Yes, technically speaking each iteration of an RFG is treated as a child post in a one-to-many post relationship with its parent post, where parent is "one" and child is "many". Each field within that RFG is treated as a postmeta entry for that RFG iteration post.

#2026653

Thank's you.