Skip Navigation

[Resolved] Calculating from a custom field and a view

This thread is resolved. Here is a description of the problem and solution.

Problem:
How can we calculate and display the results of Custom Fields in a View?

Solution:
It's natively not possible, because Toolset Views does not offer any mechanisms or GUI's to insert ShortCodes to calculate any values of Custom Fields
If you need such a feature, you should suggest this to the Product Management here https://toolset.com/home/contact-us/suggest-a-new-feature-for-toolset/.

If you want to fallback to Custom Code you might find useful examples here:
https://kb.onthegosystems.com/code-snippet/?wpv_post_search&wpv-post_tag=calculate&wpv_view_count=28

This support ticket is created 5 years, 5 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.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- - 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00
- - - - - - -

Supporter timezone: Asia/Ho_Chi_Minh (GMT+07:00)

This topic contains 4 replies, has 2 voices.

Last updated by Timothy 5 years, 5 months ago.

Assisted by: Beda.

Author
Posts
#1268939

I'm trying to calculate the number of existing posts (for example blog posts) to a custom number field (Goal Number of Blog Posts). So basically a progress meter. I have a view that calculates the number of Posts using [wpv-found-count] and I'm trying to use that View in another view to output the calculation:

[calculate] [wpv-view name="number-of-blog-posts"]/[types field='goal-number' output='raw'][/types]*100 [/calculate]

I have the general calculations function working, I added this to my functions.php:

function calculate_shortcode($atts,$content=null) {
  $content = wpv_do_shortcode($content);
  $content = eval("return $content;");
 return round($content);
}
add_shortcode('calculate', 'calculate_shortcode');

This is the error that happens when I try that:

Parse error: syntax error, unexpected '<', expecting ';' in <b>/home/hometita/public_html/sitename.com/wp-content/themes/astra-child/functions.php(221) : eval()'d code</b> on line <b>4</b>

Tim

#1269121

Toolset Views is not able to calculate, and there are no plans to activate such a feature anytime soon.
I'd like asking you to suggest this directly to the Product Management here https://toolset.com/home/contact-us/suggest-a-new-feature-for-toolset/.

For now, you'll have to resort to Custom Code solutions, for which we have 2 Example Snippets. Note that we cannot assist Custom Code as per Support policy, but we can give examples and help review code using Toolset and WordPress API.
The examples are located here:
hidden link

Related to the Code you show, the error you see happens because of a "<" returned in the $content and that means, somewhere in between the opening and closing tags of the Custom Calculation ShortCode there is a "<".
It's likely from [wpv-view name="number-of-blog-posts"], since you cannot just input a View as a calculation operand, that view outputs a whole lot of HTML and Content, unless you strip it to the minimum, but still then it usually will output a lot of data, and math cannot work with many values as one operand, you'd then have to repeat the command for each item in that loop instead of using a loop as an operand of the math.

In any case, I also wouldn't recommend that code since using eval() and being relatively an old example from our forums, I'd fall back to the examples linked above.

Please let me know if you need help with those.

BTW, to count posts we have other examples as well:
hidden link
Maybe those examples already have what you need!

#1269165

Thanks, that got me in the right direction. I just created a shortcode for each of the cpts I want to count like:

// Get post count for trainings cpt
add_shortcode('postcount-training', 'post_count1');
function post_count1() {
    $count_posts = wp_count_posts('training');
    $published_posts = $count_posts->publish;
    return $published_posts . ' ';
} 

This works.

But you mention that this calculate code is old... but I can't find newer code in the forums:

function calculate_shortcode($atts,$content=null) {
  $content = wpv_do_shortcode($content);
  $content = eval("return $content;");
 return round($content);
}
add_shortcode('calculate', 'calculate_shortcode');

Tim

#1269735

I am not sure what you mean, the Code I shared is just one example, stored on the online snippets library, the forum at Toolset, however, has plenty of other tickets with other examples, which sometimes may be from the snippets library but sometimes not, like https://toolset.com/forums/topic/nested-view-with-conditional-sums/ or others.

Note that Toolset Support does not assist Custom Code.
We can only assist how to use our API, hence the examples of these forums are often very minimal.

The code you found is a code that often was suggested in past but we do not anymore, given the usage of eval() which is, of course, no issue here given it's all at hands of the admin, but still, it's not a good practice.

Code snippets as you find them here in the forum are created by Supporters and other users, hence they must be used with certain care and understanding for their possible issues.

With the last method you shared you should be good to go, it uses all inbuilt API of WordPress 🙂

#1269777

My issue is resolved now. Thank you!