Skip Navigation

[Resolved] Need help about calculate function on Views

This support ticket is created 6 years, 3 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
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 1 reply, has 2 voices.

Last updated by Christian Cox 6 years, 3 months ago.

Assisted by: Christian Cox.

Author
Posts
#1111312

Hi,

I have succeed to use this plugin (Calculate values with shortcodes) on a views loop. https://wordpress.org/plugins/calculate-values-with-shortcodes/

I use simple sum math function (+) that calculated custom field values from types shortcode on a custom post type using this code:

<wpv-loop>
[calculate]
[type field="customfield_1_from_customposttype_1"][/types] + [type field="customfield_2_from_customposttype_1"][/types]
[/calculate]
<wpv-loop>

But, i got error when calculate custom field accross different custom post type using this code:

<wpv-loop>
[calculate]
[type field="customfield_1_from_customposttype_1"][/types] + [type field="customfield_1_from_customposttype_2"][/types]
[/calculate]
<wpv-loop>

Result:
Warning: unexpected operator '+' in /home/xxxxx/public_html/wp-content/plugins/calculate-values-with-shortcodes/evalmath.class.php on line 360

How can I fix this? Is there any way to calculate custom fields accross different custom post type without using 3rd party plugin?

#1111714

Hi, I'm not educated about that plugin so I'm not able to offer advice on how to make it work. I can help you access the values from custom fields, though. Right now I can see that you're trying to use Types field shortcodes. There are a couple of problems here:
1. In your examples, the opening shortcode is "type" but the closing shortcode is "types". They should both be "types". It was probably just a typo.
2. You should use the "raw" output type in each shortcode to get the raw value from the database. Without the raw output type, the shortcode may include extra spaces and HTML markup that could cause problems.

So first, I would test these updated shortcodes outside of the calculate function:

[types field="customfield_1_from_customposttype_1" output="raw"][/types] + [types field="customfield_2_from_customposttype_1" output="raw"][/types]

Confirm that you see the numbers you expect on the front-end, separated by " + ". If you see the numbers you expect, then you can try wrapping them with the calculate shortcode. If that doesn't work, I have another calculation snippet you can try.