I have a custom post type "Words".
Inside of Words I have 2 custom numeric fields that I need to get a total sum of all: BookWords & OtherWords
This is something that seems like it should be simple, but the closest I've come to getting it to work is getting "0". The current total should be 5927.
Is there any documentation that you are following?
https://toolset.com/forums/topic/calculating-fields/
Here's what I currently have in my functions.php:
add_shortcode('sum-total', 'sum_total_func');
function sum_total_func($atts)
{
extract( shortcode_atts( array(
'val1' => 0,
'val2' => 0
), $atts ) );
$number = $val1 - $val2;
return $number;
}
Here's the shortcode I'm using:
[sum-total val1="[types field="BookWords" output="raw"][/types]" val2="[types field="OtherWords" output="raw"][/types]"][/sum-total]
Hello,
It needs custom codes, I suggest you try the workaround of our knowledge base:
hidden link
That seems like it should work, but I'm getting an odd message instead.
I put this in my functions:
// Calculate shortcode
add_shortcode('wpv-calculate', 'calculate_shortcode');
function calculate_shortcode($atts) {
return wpv_condition($atts);
}
I'm using this in my shortcode:
[wpv-calculate evaluate=" [BookWords] + [OtherWords] "]
And this is the message I'm getting on the page now:
Correct conditional expression has not been found + [OtherWords] "]
What am I missing? This seems like it should be a super-simple thing to do.
I also tried the shortcode in this format:
[wpv-calculate evaluate=" [types field="BookWords"][/types] + [types field="OtherWords"][/types] "]
with the same result:
Correct conditional expression has not been found[/types] + "]
Please provide a test site with the same problem, also point out the problem page URL and where I can edit your custom PHP codes, I need to test it in a live website, private message box enabled
Thanks for the details, I have done below modifications in your website:
1) Create a post view "sum-of-2-numeric-custom-fields":
hidden link
- Query Words posts
- In view's loop display the sum value as blew:
Sum: [wpv-calculate evaluate=" [types field='bookwords' format='FIELD_VALUE'][/types] + [types field='otherwords' format='FIELD_VALUE'][/types] "]
If you only want to display current logged-in user's Words posts information, you can add a author filter in it, see our document:
https://toolset.com/documentation/user-guides/views/filtering-views-query-by-author/
Post author is the same as the logged in user
2) Display above post view in the problem page:
hidden link
[wpv-view name="sum-of-2-numeric-custom-fields" cached="off"]
Test it in front-end, it works fine:
hidden link
That did it! Thank you, Luo!! That is AWESOME! After so many days trying to hammer it out on my own (should have just come to the experts from the beginning) 🙂 Now I can get this site finished and running! And thank you for including the info about restricting it to a single, logged-in user as well, because that was the very next thing I'll be working on.
One quirky thing I'm seeing on this...
When another author comes in and adds more data (submits their word count for any particular day)... then the output is showing two separate numbers.... not the combined total of all of those two fields. (see attached image)
So, for one particular day the total was 5927... and a separate day the total was 4512.... but the output, as a number, I'm actually needing to see from this calculation is 10,439 – the total sum of all data in those two columns (bookwords & otherwords). And this is going to be changing frequently as I'll have multiple users inputting multiple word counts every day.
I see what you did with the Views to get where we're at now, but not sure how to modify that to get the SUM total of all data in both fields.
Thanks again!!
The credentials your provided above was removed to protect your privacy:
https://toolset.com/forums/topic/sum-of-2-numeric-custom-fields/#post-1966871
Please provide it again, also point out the problem URLs, thanks