Skip Navigation

[Resolved] Sum custom field values

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

Problem:
How to calculate fields values with Views?

Solution:
Not possible without custom code
https://toolset.com/forums/topic/display-sum-of-custom-field/#post-616972

This support ticket is created 6 years, 2 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 2 replies, has 2 voices.

Last updated by prasadS 6 years, 2 months ago.

Assisted by: Beda.

Author
Posts
#616875

Tell us what you are trying to do?

I have a custom post type namesd 'Order Counts'. (It is a child post of 2 custom post types - 'Activities' and 'Stays')

In this custom post type I have a custom field called 'Persons' (it is a number field).

I want to get the sum of the 'Persons' custom field and store it in a custom field on the parent post.

This way, I want to display the sum of the 'Persons' on the parent post.

Example:
This post hidden link
has 2 'Order Counts' child posts.

The value in the 'Persons' field in the child posts is 4 and 20.

I want to display the total, that is '24', on the parent post.

What is the link to your site?
hidden link

Kindly help.

#616972

This falls under custom code, that we do not deliver ready to use.
https://toolset.com/toolset-support-policy/

If you require help with advanced custom code, we suggest to contact a contractor:
https://toolset.com/contractors/

But I can go over the theory and the API's to use with you here.

1. You need as first step to get all children posts that have the same parent

This can be done with a get_posts() query, where the important is to query for posts where the hidden Custom Field "_wpcf_belongs_your-parent-post-type-slug_id is the same (The shared Parent ID).
https://codex.wordpress.org/Template_Tags/get_posts

You can also use our own API as elaborated here to get children or parent posts, but those are just wrappers for the native get_posts() of WordPress:
https://toolset.com/documentation/user-guides/many-to-many-post-relationship/#displaying-many-to-many-relationship-using-types-php-api

2. Once you have the Posts, you need to get the Field data with get_post_meta() foreach() of the posts that above get_posts() returned.
https://developer.wordpress.org/reference/functions/get_post_meta/
hidden link

3. Then, you sum them up, using simple PHP Math operators.

4. After, you update_post_meta() the target field in the parent post with this summary:
https://codex.wordpress.org/Function_Reference/update_post_meta

The last question is when you do this.
In the backend, you will use save_post() WordPress Hook.
In that case you need to pay attention to this:
https://toolset.com/documentation/customizing-sites-using-php/updating-types-fields-using-php/

If you use CRED; you can use the cred_save_data() hook:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data

Please let me know if you need more detailed informations on this.

#617887

Thank you for the help 🙂

I will soon consider getting tis done with help from one of the contractors.

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