Skip Navigation

[Resolved] How to count repeated field?

This support ticket is created 6 years, 7 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
- - 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00
- - - - - - -

Supporter timezone: Asia/Ho_Chi_Minh (GMT+07:00)

This topic contains 5 replies, has 2 voices.

Last updated by Beda 6 years, 7 months ago.

Assigned support staff: Beda.



I need to count a specific repetitive field in a post.

I followed this thread which describes the same issue for me but the code isn't giving right results:

Any help will be appreciated.



One possibility is to output the Fields as numbered Lists
For that you can use simple HTML ol/li and the Views ShortCode wpv-for-each:

That will of course not display the total of items but a number of subsequent items.

For a simple Total numerical value of repeating Fields, outputted on a single Post, you can use the code that Caridad provided.

Insert to functions.php:

// shortcode to count number of repeating field instances
add_shortcode( 'count-repeats', 'count_repeat_func' );
function count_repeat_func($atts) {
    return sizeof(get_post_meta( get_the_ID(), 'wpcf-' . $atts['field'], false ));

Use in the Single Post:

[count-repeats field="your-field-slug"]

Hello Beda,

I added the code to functions.php and used [count-repeats field="your-field-slug"] after changing "your-field-slug" but it didn't give right results.

I am using [count-repeats field="your-field-slug"] in a View not in a single post.



Yes, this will not work.
The code is designed to work on a single post only.
It uses get_the_ID():

To use it in the View, you should pass the Post ID as a ShortCode attribute.

You can do that the same way as we passed the "Field" (Slug of the field).

That means we pass a second argument and call it with $atts in our code.
Then we pass the Toolset ShortCode [wpv-post-id] in the ShortCode itself, used in the Loop.

This means your code looks like this after:

add_shortcode( 'count-repeats', 'count_repeat_func' );
function count_repeat_func($atts) {
    return sizeof(get_post_meta( $atts['post-id'], 'wpcf-' . $atts['field'], false ));

Then you can use this ShortCode:

[count-repeats post-id="[wpv-post-id]" field="url"]

You should wrap that ShortCode in a HTML Conditional to check if the Field does exist, as other wise unexpected results can be produced:


Hello Beda,

It still give false results.

I have a field repeated 10 times but the above code give a "1" result instead of "10".

If you enable private reply I can provide a link.


10 Repeating Fields.png

This does work as expected.

I do have a Post with 10 (also tried less and more) Repeating URL Fields.

I have the above function in my functions.php and used the ShortCode in my View that Lists posts.

The output is correct.

Please see the ScreenShots.
(It is the Post 21 in my screenshot that has 10 fields)

It could be possible that you need custom programming work which is beyond the scope of our support.

At this point I would suggest you consider contacting one of our certified partners from this link:

You will get the custom assistance you need to get on with your project.

This is because I can not debug Custom Code or custom Solutions
I can provide Guidance how to achieve it, and give the proper documentation if necessary.