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>"));
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.
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;
}
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.