Skip Navigation

[Closed] SUM of 2 numeric custom fields

This support ticket is created 3 years, 1 month 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 1.27 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/Hong_Kong (GMT+08:00)

This topic contains 8 replies, has 2 voices.

Last updated by Luo Yang 3 years ago.

Assisted by: Luo Yang.

Author
Posts
#1964795

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]

#1964961

Hello,

It needs custom codes, I suggest you try the workaround of our knowledge base:
hidden link

#1965467

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.

#1965471

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] + "]
#1966177

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

#1967483

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

#1967989

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.

#1968363
HMWT-Screenshot 2021-02-26 125046.png

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!!

#1970015

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

The topic ‘[Closed] SUM of 2 numeric custom fields’ is closed to new replies.