Skip Navigation

[Resolved] Using math with custom fields across multiple posts

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

Our next available supporter will start replying to tickets in about 2.44 hours from now. Thank you for your understanding.

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

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

This topic contains 1 reply, has 2 voices.

Last updated by Waqar 4 years, 2 months ago.

Assisted by: Waqar.

Author
Posts
#1478955

Tell us what you are trying to do?
I have a taxonomy that consists of company > teams > members. I have 4 values set in each custom post for the member that has a numerical value associated with it - those field are: red, blue, yellow, green. I need to add up the totals for red, blue, yellow, and green across all posts that have a specified team ID. So, for example, I have Team Member 1, Team Member 2, and Team Member 3. For each post of [team-member] that has the same team-member-id, I need to add the score for the [red], [yellow], [green], and [blue] fields.

Example:
Team Member 1: Red: 4, Green: 3, Yellow: 2, Blue: 1
Team Member 2: Green: 5, Red: 2, Blue: 2, Yellow: 1
Team Member 3: Blue: 4, Yellow: 3, Green: 2, Red: 1

Knowing this:
I need to know that the total for Green is 10 (the aggregate value for Green for each Team Member)
I need to know that the total for Blue is 7 (the aggregate value for Blue for each Team Member)
I need to know that the total for Yellow is 6 (the aggregate value for Yellow for each Team Member)
I need to know that the total for Red is 7 (the aggregate value for Red for each Team Member)

Once I have this information, I need to then calculate the percentage of each color against the total 'score' for the team, knowing that the total score available is 3 (10 for each team member).

Example:
Green Total: 10, Total Score: 30, Green Percentage: 33.33%
Blue Total: 7, Total Score: 30, Blue Percentage: 23.33%
Yellow Total: 6, Total Score: 30, Yellow Percentage: 20%
Red Total: 7, Total Score: 30, Red Percentage: 23.33%

Is there any documentation that you are following?
Maybe: https://toolset.com/forums/topic/math-calculations-with-custom-fields-of-related-posts-inside-a-view/
Maybe: https://toolset.com/forums/topic/sum-of-fields-in-a-view-2/
Maybe: https://toolset.com/forums/topic/display-sum-of-custom-field/

Is there a similar example that we can see?

What is the link to your site?
hidden link

#1480627

Waqar
Supporter

Languages: English (English )

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

Hi Josh,

Thank you for contacting us and I'd be happy to assist.

There is no built-in feature available to achieve this, but you can use custom PHP code and WordPress shortcodes API ( ref: https://codex.wordpress.org/Shortcode_API ) for this.

As much as we would like to help, 1-1 customization assistance is beyond the scope of support that we provide, but we do our best to guide in the right direction whenever possible.

The example code snippet from Beda's reply ( ref: https://toolset.com/forums/topic/sum-of-fields-in-a-view-2/#post-358787 ), is very relevant to what you need.

First It cycles through all posts in a specified post type, using the "get_posts" function.
( ref: https://developer.wordpress.org/reference/functions/get_posts/ )

Next, inside the "foreach" loop, it uses the ID of all the found posts to get the custom field value for a custom field with slug "wpcf-numeric-one" through "get_post_meta" function ( ref: https://developer.wordpress.org/reference/functions/get_post_meta/ ) and then returns the sum all those values.

In your case, you'll be getting the values of all 4 color fields and will be storing them in different variables, one for each color and also one for the total of all colors, so that the percentage can be calculated at the end.

To show the percentages for the different colors, you can either register 4 different shortcodes or use a single one, which accepts a shortcode attribute to specify which color's percentage is required and should be returned as a final output.

I hope this helps and for more personalized assistance around custom code, you can consider hiring a professional from our list of recommended contractors:
https://toolset.com/contractors/

regards,
Waqar

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