Skip Navigation

[Resolved] Display repeating field group with php

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

Problem: I would like to know how to loop over repeating field groups with PHP.

Solution:
Use the toolset_get_related_posts API. Each RFG can be accessed as a child, where the post is the parent and the relationship slug is the RFG slug.

$parent = get_post( 12345 ); // the parent post that holds the RFG
$sezioni = toolset_get_related_posts(
  $parent,     // the parent post
  'sezioni',   // the RFG slug
  'parent',     // the post role in this relationship is 'parent'
  1000000,     // the maximum number of results
  0,           // the offset
  array(),     // additional query arguments
  'post_id',   // return format
  'child',    // role to return
);
  
// $sezioni is now an array of post IDs for each RFG
  
$sez1 = get_post( $sezioni[0] ); // example, get the first Sezioni RFG ID
  
$pulsanti = toolset_get_related_posts(
  $sez1,        // the parent RFG
  'pulsanti',  // the RFG slug
  'parent',     // the sezioni role in this relationship is 'parent'
  1000000,     // the maximum number of results
  0,           // the offset
  array(),     // additional query arguments
  'post_id',   // return format
  'child',    // role to return
);
  
// $pulsanti is now an array of post IDs for each RFG in $sez1

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

This support ticket is created 5 years, 10 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 3 replies, has 2 voices.

Last updated by Christian Cox 5 years, 10 months ago.

Assisted by: Christian Cox.

Author
Posts
#918019
Schermata 2018-06-26 alle 18.05.39.png

Hi,
i create a post field group with some text field and a repeatable group (with other fields inside).

now i want to display that repeatable groud fields in my php template... how can i do?

Using:

$child_posts = types_child_posts("group-slug1");
foreach ($child_posts as $child_post) {
echo(types_render_field("field1", array( "id" => "$child_post->ID")));
echo(types_render_field("field2", array( "id" => "$child_post->ID")));
}

it works... but if i create for the same post field group another repeatable group (group-slug2) I realize the "types_child_posts" is not the right way...

I would also like to create another repeatable group fields inside the first repeatable group fields (like the screenshots)... but I have no idea how to display them in php...

can u help me?
tnx
francesco

#918047

You should use the Post Relationships API toolset_get_related_posts to loop over Repeating Field Groups (RFGs). An RFG is technically a child post in a relationship with a slug that matches the RFG slug.

$parent = get_post( 12345 ); // the parent post that holds the RFG
$sezioni = toolset_get_related_posts(
  $parent,     // the parent post
  'sezioni',   // the RFG slug
  'child',     // the RFG role in this relationship is 'child'
  1000000,     // the maximum number of results
  0,           // the offset
  array(),     // additional query arguments
  'post_id',   // return format
  'parent',    // role to return
);

// $sezioni is now an array of post IDs for each RFG

$sez1 = get_post( $sezioni[0] ); // example, get the first Sezioni RFG ID

$pulsanti = toolset_get_related_posts(
  $sez1,        // the parent RFG
  'pulsanti',  // the RFG slug
  'child',     // the RFG role in this relationship is 'child'
  1000000,     // the maximum number of results
  0,           // the offset
  array(),     // additional query arguments
  'post_id',   // return format
  'parent',    // role to return
);

// $pulsanti is now an array of post IDs for each RFG in $sez1

More information about the toolset_get_related_posts API here: https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_posts

#918250

Hi Christian,
we improved your code with our parameters.

$parent = get_post( 9 ); // 9 is the ID of our page
$sezioni = toolset_get_related_posts(
  $parent,     // the parent post
  'sezioni',   // the RFG slug
  'child',     // the RFG role in this relationship is 'child'
  1000000,     // the maximum number of results
  0,           // the offset
  array(),     // additional query arguments
  'post_id',   // return format
  'parent'    // role to return
);

print_r(get_post($sezioni));

You can see the array result at this page:
hidden link

Why the array doesn't contain the IDs of our RFG?
tnx
francesco

#918350

I think I had the parent and child roles backwards. Try this instead:

$parent = get_post( 12345 ); // the parent post that holds the RFG
$sezioni = toolset_get_related_posts(
  $parent,     // the parent post
  'sezioni',   // the RFG slug
  'parent',     // the post role in this relationship is 'parent'
  1000000,     // the maximum number of results
  0,           // the offset
  array(),     // additional query arguments
  'post_id',   // return format
  'child',    // role to return
);
 
// $sezioni is now an array of post IDs for each RFG
 
$sez1 = get_post( $sezioni[0] ); // example, get the first Sezioni RFG ID
 
$pulsanti = toolset_get_related_posts(
  $sez1,        // the parent RFG
  'pulsanti',  // the RFG slug
  'parent',     // the sezioni role in this relationship is 'parent'
  1000000,     // the maximum number of results
  0,           // the offset
  array(),     // additional query arguments
  'post_id',   // return format
  'child',    // role to return
);
 
// $pulsanti is now an array of post IDs for each RFG in $sez1
This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.