Skip Navigation

[Resolved] Count entries in repeatable group

This support ticket is created 5 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.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9: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/Karachi (GMT+05:00)

This topic contains 2 replies, has 2 voices.

Last updated by marcialB 5 years, 3 months ago.

Assisted by: Waqar.

Author
Posts
#1177553

Hi there

Can I apply this technique "https://toolset.com/forums/topic/how-to-count-repeated-field/" to repeatable groups? So that it shows me how many repetable groups I created in a post?

Thanks,
Marcial

#1177647

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hi Marcial,

Thank you for contacting us and I'll be happy to assist.

The repeatable field groups are stored differently than simple repeating fields.

To get the count of attached repeatable field group entries of a post, you can create a custom shortcode, that uses "toolset_get_related_posts" function:
( ref: https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_posts )

The following code can be added to the active theme's "functions.php" file:


function show_rfg_count_func( $atts ) {
	$a = shortcode_atts( array(
		'id' => get_the_ID(),
		'type' => '',
	), $atts );

	if ( (!empty($a['id'])) && (!empty($a['type'])) )
	{
		$section_ids = toolset_get_related_posts(
			$a['id'],
			$a['type'],
			'parent',
			1000,
			0,
			array(),
			'post_id',
			'child'
		);
		
		return count($section_ids);
	}

}
add_shortcode( 'show_rfg_count', 'show_rfg_count_func' );

To get the count, you can use the shortcode inside a post/view loop, as:


[show_rfg_count type="field-group-slug"]

Note: You'll replace "field-group-slug" with the actual slug of the repeating field group.

To get a count from a specific post, you can use:


[show_rfg_count type="field-group-slug" id="123"]

Note: You can replace "1123" with the actual ID of the post.

I hope this helps.

regards,
Waqar

#1177673

Hi there

Awesome, works just as expected.

I adjusted the shortcode with the dynamic post ID so it works in the layout for my custom post type:

[show_rfg_count post-id="wpv-post-id" type="field-group-slug"]

Thanks a lot!

Best,
Marcial

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.