Skip Navigation

[Resolved] Count number of entries in repeating field

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

Problem:

I have a field (date field) that records the date every time that a client takes a class, What I really need is an "at a glance" count. So, I would like this view to display instead: count number of multiple instance field.

Solution:

There isn't such a built-in feature, as a workaround, you can create a custom shortcode for it, like this:

https://toolset.com/forums/topic/count-number-of-entries-in-repeating-field/#post-1207355

Relevant Documentation:

This support ticket is created 5 years, 9 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/Hong_Kong (GMT+08:00)

This topic contains 2 replies, has 2 voices.

Last updated by sarahK-2 5 years, 9 months ago.

Assisted by: Luo Yang.

Author
Posts
#1207217

I have a field (date field) that records the date every time that a client takes a class. I am able to show these in a view as a numbered list to easily see how many classes they have taken. However, it takes up a lot of room.

So, right now an attendance record on this view looks like this:

Client Name, etc
Classes Taken:
1. Date
2. Date
3. Date
4. Date
5. Date

What I really need is an "at a glance" count. So, I would like this view to display instead:

Client Name, etc
Classes Taken: 5

Thanks in advance. This is not a repeating field group, it is just a repeating field (allow multiple instances...)

#1207355

Hello,

There isn't such a built-in feature, as a workaround, you can create a custom shortcode for it, like this:
1) Add below codes into your theme/functions.php:

add_shortcode('count_instances', 'count_instances_func');
function count_instances_func($atts, $content){
    $atts = shortcode_atts( array(
        'field' => '',
        'post_id' => get_the_ID(),
    ), $atts );
     
    $field = get_post_meta($atts['post_id'], $atts['field'], false);
    $res = 0;
    if(is_array($field)){
        $res = count($field);
    }
    return $res;
}

2) display the count result with above shortcode, like this:
[count_instances field="wpcf-my-field"]

You just need to replace "my-field" with your custom field slug.

#1207719

As usual, amazing support 🙂

Thanks!