Skip Navigation

[Resolved] Count multiple instances

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

Problem:
I am using multiple instances field OR repeating field to upload photos. I would like to count these photos and show on the page that there are X photos for this post or listing.

Solution:
You can do this by using custom shortcode. Please add this code in your theme’s or child theme’s functions.php file:

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. You should wrap this Shortcode in an HTML Conditional to check if the Field does exist, as other wise unexpected results can be produced:

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

More details: https://toolset.com/forums/topic/how-to-count-repeated-field/#post-412707

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

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 12:00 – 17:00 12:00 – 17:00 12:00 – 17:00 12:00 – 17:00 12:00 – 17:00 -
- 18:00 – 21:00 18:00 – 21:00 18:00 – 21:00 18:00 – 21:00 18:00 – 21:00 -

Supporter timezone: Asia/Karachi (GMT+05:00)

This topic contains 4 replies, has 2 voices.

Last updated by Adrian 7 years, 4 months ago.

Assisted by: Noman.

Author
Posts
#549996

I am using a multiple instances field to upload photos. I would like to be able to count these photos and announce on the page that there are x photos to this listing.

What is the variable/function that would allow me to do so?

#550002

Noman
Supporter

Languages: English (English )

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

Hi Adrian,

Thank you for contacting Toolset Support. We can achieve this by using custom shortcode. Please add this code in your theme’s or child theme’s functions.php file:

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. You should wrap this ShortCode in an HTML Conditional to check if the Field does exist, as other wise unexpected results can be produced:

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

More details: https://toolset.com/forums/topic/how-to-count-repeated-field/#post-412707

Thank you

#550004

Hi Noman, that is really useful. Thank you!
Is there a way to count child posts?

If a listing has x floor plans, display that number.

Thank you

#550022

Noman
Supporter

Languages: English (English )

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

Great to hear that it helps.

Is there a way to count child posts?
==> Yes this is possible, but please kindly open a new ticket for this and assign it directly to me (or just share the link to new ticket in this thread & I will reply on it). This will help other users with similar problems to find solutions when searching the forum, I am not allowed to handle multiple issues in the same ticket as per support policy. https://toolset.com/toolset-support-policy/

Thank you

#550023

Will do thank you