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