Skip Navigation

[Resolved] How to display repeating field groups with PHP

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

Problem: I would like to display repeating field groups (RFGs) using only PHP.

Solution: Use the Post Relationships API or the toolset_relationships query argument to retrieve RFGs from the database and loop over them.

$args = array(
  'post_type' => 'book-rfg',
  'numberposts' => -1,
  'toolset_relationships' => array(
    'role' => 'child',
    'related_to' => 12345,
    'relationship' => 'book-rfg'
  ),
);
$the_query = new WP_Query( $args );
while ( $the_query->have_posts() ){
  $the_query->the_post();
  the_title();
  echo '<br />';
}

Relevant Documentation:

50% of people find this useful.

This support ticket is created 6 years, 5 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 6 years, 5 months ago.

Assisted by: Christian Cox.

Author
Posts
#908840

Hello,

im looking for the way to display my repeatable groups field with PHP in my templates.

I don t find any documentation on toolset website.

I know how display a simple or repeatable fields, but can t find a solution for groups.

Can you help me ?

Thanks

#909132

Hi, the Views API render_view is designed to help you display any View using PHP. You can build a View of your RFGs and test it out by inserting it in a Content Template or post, then once it's working correctly you can use the render_view API to output the same View with PHP. Here's an example View shortcode:

[wpv-view name="rfgs-in-a-book" limit="3" offset="1" orderby="title"]

Here's the same View rendered with PHP:

$args = array(
    'name' => 'rfgs-in-a-book',
    'limit' => '3',
    'offset'=> '1',
    'orderby'=>'title',
);
echo render_view( $args );

Here's the documentation for this API: https://toolset.com/documentation/programmer-reference/views-api/#render_view

#909470

Thanks, i ll do it by creating a view.

But my question was to do this without Views, just with php. Not possible?

#909473
Screen Shot 2018-06-06 at 12.36.27 PM.png
Screen Shot 2018-06-06 at 12.37.50 PM.png

Repeating Field Groups are basically posts in a post relationship that is defined by the RFG slug. This document shows a WP_Query to find posts in a relationship:
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/how-to-migrate-your-site-to-new-post-relationships/

Here's a basic example with an RFG with the slug "book-rfg" and a parent post with ID 12345:

  $args = array(
    'post_type' => 'book-rfg',
    'numberposts' => -1,
    'toolset_relationships' => array(
      'role' => 'child',
      'related_to' => 12345,
      'relationship' => 'book-rfg'
    ),
  );
  $the_query = new WP_Query( $args );
  while ( $the_query->have_posts() ){
    $the_query->the_post();
    the_title();
    echo '<br />';
  }

The results of $the_query will contain all the child RFGs of the parent Book with ID 12345. Then you can use get_post_meta to find any custom field values within that RFG. The post title will be the name you give to each row of the RFG in wp-admin. Attaching some screenshots here showing the editor in wp-admin and the results on the front-end.