Skip Navigation

[Closed] Counting values in custom field

The Toolset Community Forum is closed, for technical support questions, please head on to our Toolset Professional Support (for paid clients), with any pre-sale or admin question please contact us here.
This support ticket is created 9 years, 5 months ago. There's a good chance that you are reading advice that it now obsolete.
This is the community support forum for Types plugin, which is part of Toolset. Toolset is a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients and people who registered for Types community support can post in it.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 18:00 9:00 – 18:00 9:00 – 18:00 9:00 – 18:00 9:00 – 18:00 -
- - - - - - -

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

This topic contains 3 replies, has 2 voices.

Last updated by Waqas 9 years, 5 months ago.

Assisted by: Waqas.

Author
Posts
#259489

I have a custom field that allows the user to enter a number value (in this case, the number of attendees at an event.) I'd like to display reporting on my site that shows the total number of attendees at all the events in each state.

Is it possible through Toolset to get a count of the values of several posts? Specifically, I'm trying to figure out how to show the total number of attendees in each state. So I would need to query all the posts by state, then count the values in the attendees fields of those records. Possible?

Thanks.

#259616

Waqas
Supporter

Languages: English (English )

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

Although this is not offered by a Toolset short code so far, but you can achieve this very easily. Please add following code to your active theme's 'functions.php' file:

global $total;
add_shortcode('add-to-total', 'add_total_shortcode');
function add_total_shortcode($atts, $content = '') {
	global $total;
	
	$total += wpv_do_shortcode($content);
}

add_shortcode('show-total', 'show_total_shortcode');
function show_total_shortcode() {
	global $total;
	
	$totalNew = $total;
	$total = 0;
	return $totalNew;
}

This will add 2 new short codes to your system; [add-to-total] and [show-total]. The usage of these short codes will be as below (sample code):

<wpv-loop>
 <li>[wpv-post-title] ([types field="attendees" id=""][/types])
   [add-to-total][types field="attendees" id=""][/types][/add-to-total]
</li>
</wpv-loop>
Total Attendees: [show-total]

Please notice, the use of [add-to-total]...[/add-to-total]. You will need to supply your "number of attendees" field as a content of this short code and will need to use it in View's loop (as shown above).

Please let me know if I can help you with anything related.

#259708

Thanks for the quick response! This works beautifully. I can now filter my results by state and see the total for that state.

Do you have an idea how I might also create a view that loops through all the states and lists the totals for each state, all together? Like:

Arizona: 10
Connecticut: 34
Delaware: 9

etc...

#259775

Waqas
Supporter

Languages: English (English )

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

If "state" is a taxonomy in your CPT architecture, you can use [wpv-taxonomy-post-count] short code, in a taxonomy view loop, details are at https://toolset.com/documentation/views-shortcodes/#wpv-taxonomy-post-count and several other useful short codes.

However, if state is a custom field, you can create custom short codes, to query on the particular state in the loop and getting the number of posts.

Please let me know if I can help you with anything related.

The topic ‘[Closed] Counting values in custom field’ is closed to new replies.