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
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
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