Skip Navigation

[Resolved] Display repeating fields group using PHP

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

Problem:
Display repeating fields group using PHP

Solution:
To query Repeating fields group items we have PHP API function which you should use now: toolset_get_related_posts()
You can find proposed solution, in this case, with the following reply:
https://toolset.com/forums/topic/display-repeating-fields-group-using-php/#post-955403

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

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

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Kolkata (GMT+05:30)

This topic contains 4 replies, has 2 voices.

Last updated by eddieW-2 6 years, 3 months ago.

Assisted by: Minesh.

Author
Posts
#955339
Repeating Field Group.PNG

Hi, I am trying to display post fields group in my templates. I do not wish to use Views, preferably with PHP. My questions are as following:

1) How do I display all custom fields (repeating groups included) by just using the Post Field Group ID (the ID that appears on the URL when editing the group)? So that I do not require to call out each custom fields in the group one by one.

2) If the above is not possible, how do I display a repeating field group in the Post Field Group using slug in PHP within an existing WP_Query loop?

I have already tried to follow this thread (https://toolset.com/forums/topic/how-to-display-repeatable-field-groups-with-php/) but when I try to var_dump the query object, the contents of the custom field doesn't appear. Am I missing something?

$args = array(
'post_type' => 'home-section',
'numberposts' => -1,
'toolset_relationships' => array(
'role' => 'child',
'related_to' => get_the_ID(),
'relationship' => 'home-section'
),
);
$the_query = new WP_Query( $args );

#955403

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

Well - to query Repeating fields group items we have PHP API function which you should use now: toolset_get_related_posts()

For example:

global $post;

 $child_columns = toolset_get_related_posts(
  $post->ID,     // the parent post
  'columns-layout',   // the RFG slug
  'parent',     // the RFG role in this relationship is 'child'
  100,     // the maximum number of results
  0,           // the offset
  array(),     // additional query arguments
  'post_object',   // return format
  'child',    // role to return
  '',
  'ASC'
  );
#955424

Hi Minesh, thanks for the quick reply.

By using toolset_get_related_posts(), I can retrieve the group ID but how do I retrieve the content of the field? Do I use get_post_meta() to retrieve the values or is there another function that can do so easily?

#955438

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Yes - you can use get_post_meta() function or Types PHP API function types_render_field().

For example:

echo types_render_field( 'field-slug', array( 'arg1' => 'val1', 'arg2' => 'val2' ));

More info:
=> https://toolset.com/documentation/customizing-sites-using-php/functions/
=> https://toolset.com/documentation/customizing-sites-using-php/

#955451

I got it working using get_post_meta. Thanks for your help!

$section_ids = toolset_get_related_posts(
	get_the_ID(),
	'home-section',
	'parent',
	100,
	0,
	array(),
	'post_id',
	'child'
);

if ( $section_ids ){
	foreach ( $section_ids as $section_id ){
		$section = get_post_meta( $section_id, '', false );
		$section_content = $section["wpcf-section-editor"][0];
		echo apply_filters('the_content', $section_content);
	}
}