Skip Navigation

[Resolved] How to count repeated field?

This support ticket is created 7 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
- - 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 7 years, 9 months ago.

Assisted by: Beda.

Author
Posts
#412032

Hello

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:
https://toolset.com/forums/topic/show-count-of-repeating-field-entries/

Any help will be appreciated.

Thanks.

#412049

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:
https://toolset.com/documentation/views-shortcodes/#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"]
#412145

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.

Thnaks

#412707

Yes, this will not work.
The code is designed to work on a single post only.
It uses get_the_ID():
https://developer.wordpress.org/reference/functions/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:
https://toolset.com/documentation/user-guides/conditional-html-output-in-views/
https://toolset.com/documentation/user-guides/conditional-html-output-in-views/checking-types-fields-and-custom-fields/
https://toolset.com/documentation/user-guides/conditional-html-output-in-views/checking-fields-and-other-elements-for-emptynon-empty-values/

#412714

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.

Thanks

#412719
View.png
Output.png
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:
https://toolset.com/consultant/

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.
https://toolset.com/toolset-support-policy/

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