Skip Navigation

[Resolved] Number fields sum for specific post types and fields

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

This topic contains 10 replies, has 2 voices.

Last updated by Arrien 7 years, 3 months ago.

Assisted by: Francesco Di Candia.

Author
Posts
#528554

I am wondering if it's possible to do the following.

A general function that calculates the sum (calc-sum)
Targets 2 post types: material-checkout and check-in
Then we can create shortcodes like this:

$[calc-sum field='material-weight-in']
$[calc-sum field='material-weight-out']
$[calc-sum field='money-saved']

Thank you.

#529693

Hello Arrien,
thank you for contacting the Toolset support.

I need some more information about your question to help you in the best way.

  • both Custom Post Type have the same ‘Custom Field’ with the same name in each CPT?
  • you need to have the sum of ‘material-weight-in’ and all the other CF in all the CPT or you need to choose which CF in CPT you want to sum?

If you need only to sum all the ‘material-weight-in’ CF (and all the others CF) in both the CPT, you can create a function, that should do that, that you’ve to register as a Third Party Shortcode

  • Go to Toolset->Settings and click on the Front-end Content tab
  • Look for the section called Third-party shortcode arguments

Read the documentation here https://toolset.com/documentation/user-guides/shortcodes-within-shortcodes/

If this is not what you are asking, can you please elaborate a little more so I can better understand?
Sometimes screenshots illustrating what you mean may go a long way to clarify what words fail to transmit so if you like make use of "upload an image" below the comment area.

Thank you, Francesco

#529863

Hi Francesco,

I have the CPT "material-checkout" with the following CF's that I would like to calculate the sums of:

material-weight-out
money-saved

I also have a CPT "check-in" with the following CF that I would like to calculate the sum of:

material-weight-in

Does that answer your question?

Thank you.

#530296

Hello Arrien,
so, as I can read, you’ve to create a shortcode that can execute the sum for you.
As I wrote before, you can do it creating a shortcode and a function for it in your functions.php file.

First of all you’ve to add a code like this:

function count_numeric_custom_field_func( $atts ) {
    global $wpdb;
    extract( shortcode_atts( array(
        'field' => '',
        'decimals' => 2,
        'format_decimals' => 2,
    ), $atts ) );
      
    $count = $wpdb->get_var( $wpdb->prepare(
        "
            SELECT SUM(CAST(meta_value AS DECIMAL (15,{$decimals}))) as count
            FROM $wpdb->postmeta 
            WHERE meta_key = %s
        ",
        $field
    ) );
    if (empty($count)){
        $count = 0;
    }
  
    return number_format($count, $format_decimals);
}
add_shortcode( 'count_numeric_custom_field', 'count_numeric_custom_field_func' );

in your functions.php file.

This code declares a shortcode

[count_numeric_custom_field]

that you can use where you want to display your sum, for instance in a page.
As you can read, that code sums all the $field instances in the database, so you can use in a page writing like so

[count_numeric_custom_field field='wpcf-material-weight-in']

where wpcf-material-weight-in is your Custom Field (remember that you need to put wpcf- just before your slug).
Optionally you can choose and pass to the function the ‘decimals’ and the ‘format_decimals’.

Please let me know if this solves your problem.
Thank you, Francesco

#530548

Hi Francesco,

I have setup the shortcode like you described, but it does not seem to be calculating correctly.

I have exported a csv file of the same fields I want to calculate the sum of and the total is off by almost 1000.

#530769

Hello Arrien,
the issue you're experiencing is about an error on calculation?
To help you to solve this issue, I would like to ask you for credentials to your website and FTP.
It will help me debug the issue and find the solution.
You will find the fields for WordPress and FTP access below the comment area when you log in to leave your next reply.
Thank you, Francesco.

#531082

Hello Arrien,
I'm checking with the database tables and everything seems fine.
I need to know how to you exported the numbers to an Excel file so I can check where is the problem.
Are you speaking about an Excel file exported from your site? If so, what is the procedure you've done to do that?
Can you please use a service as Dropbox to upload that file so I can check it?
I'll enable private reply so you can attach the link.
Thank you, Francesco.

#532888

Hi Arrien,
I checked your database and now we have to modify our query in order to use only the correct CPT for each CF and to use only the published CPT one.
So I modified the query (I've commented the old one in your functions.php)

$count = $wpdb->get_var( $wpdb->prepare(
        "
            SELECT SUM(CAST(meta_value AS DECIMAL (15,{$decimals}))) as count              
              	FROM $wpdb->posts p 
              	INNER JOIN $wpdb->postmeta pm          
	    ON p.id = pm.post_id
		WHERE p.post_type = %s
		AND p.post_status = 'publish'
	        AND meta_key = %s
        ",
	$cpt,
        $field
    ) );

As you can see, we use also the posts table so we can check the post_type and the post_status and we will use the CF that becomes from the correct CPT.
Our shortcode will change accordingly so

[count_numeric_custom_field field='wpcf-material-weight-in' cpt='check-in']

where CPT is the Custom Post Type from which the CF comes.
I've already modified your functions.php file and your hidden link page.
Please, let me know if this solves your issue.
Francesco

#533040

Hi Francesco,

Thanks!

Seems to work with the CPT check-in, but not for material-checkout.

I changed the field name material-weight-out from material-weight.

Is that causing it to be 0.00 on the hidden link page?

Thanks.

#533209

Hi Arrien,
you can't change the CF name without issues.
If you want the query will work, you've to use the correct CPT and the correct CF used in that CPT when you saved the CPT.
If you change the CF name you will not have the sum of the previous CF saved with the CPT.

Thank you, Francesco

#533358

Thank you Francesco.

I realize my error now. I think I will have to manually rebuild the CPT and CF's then since I don't remember what the original slug names are. At least I don't have too many CPT's, yet.

This ticket is now closed. If you're a Toolset client and need related help, please open a new support ticket.