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 enlace oculto
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?
enlace oculto
Kindly help.
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/
enlace oculto
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.
Thank you for the help 🙂
I will soon consider getting tis done with help from one of the contractors.