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

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.

This topic contains 2 replies, has 2 voices.

Last updated by prasadS 3 years, 5 months ago.

Assigned support staff: Beda.


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.

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.


This falls under custom code, that we do not deliver ready to use.

If you require help with advanced custom code, we suggest to contact a contractor:

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).

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:

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.
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:

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:

If you use CRED; you can use the cred_save_data() hook:

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


Thank you for the help 🙂

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