Skip Navigation

[Resolved] Two custom Toolset codes are not working together

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

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
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 12 replies, has 2 voices.

Last updated by Arrien 7 years, 1 month ago.

Assisted by: Christian Cox.

Author
Posts
#556967
Screen Shot 2017-08-06 at 10.22.35 AM.png
Screen Shot 2017-08-06 at 10.22.15 AM.png

Hello,

I have been a lot with Toolset over the last few months to create a fairly unique system for a creative reuse space.

I have two custom codes that have been made by the Toolset support team and they have worked great up until now.

I have one custom code that saves custom fields as the CPT title.
https://toolset.com/forums/topic/custom-post-types-generate-their-own-post-titles-from-1-or-2-custom-fields/

//Save Member name as post title.
add_action('cred_save_data','func_custom_post_title', 10, 2);
function func_custom_post_title($post_id,$form_data) {
$arr = array(12, 13); // here you can add more CRED form IDs
if (in_array($form_data['id'], $arr)) {
$type = get_post_meta($post_id, 'wpcf-member-type', true);
$title = '';
if ($type == 'individual') {
$firstname = get_post_meta($post_id, 'wpcf-member-first-name', true);
$lastname = get_post_meta($post_id, 'wpcf-member-last-name', true);
$title = $firstname. ' ' . $lastname;
}
if ($type == 'group') {
$groupname = get_post_meta($post_id, 'wpcf-member-group-name', true);
$title = $groupname;
}
if($title){
$slug = sanitize_title($title);
$args = array(
'ID' => $post_id,
'post_title' => $title,
'post_name' => $slug
);
wp_update_post($args);
}
}
}

I have another custom code that calculates the sum of custom fields
https://toolset.com/forums/topic/number-fields-sum-for-specific-post-types-and-fields/

//Calculate membership fields
add_action("wpt_field_options", 'changenotset', 10, 3);

function count_numeric_custom_field_func( $atts ) {
global $wpdb;
extract( shortcode_atts( array(
'field' => '',
'cpt' => '',
'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->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
) );
if (empty($count)){
$count = 0;
}

return number_format($count, $format_decimals);
}

add_shortcode( 'count_numeric_custom_field', 'count_numeric_custom_field_func' );

I have setup the two codes as separate plugins. However, when I activate the second, "calculate the sum of custom fields", form fields on the front end of the site disappear. I have attached screenshots.

I just did another test and it is when only the second code is activated that the front end form fields disappear. Is it maybe interfering with form functionality?

Please advise.

#557038

Hello, I see the following action in the 2nd plugin, but I do not see the function 'changenotset' defined anywhere:

//Calculate membership fields
add_action("wpt_field_options", 'changenotset', 10, 3);

I think you may have deleted some code, or not copy + pasted the entire code, because there should be a function called 'changenotset'. Since it's not defined, all the field options are being deleted. Double check the code from your other tickets and make sure you have copied all the relevant code over into your plugins.

#557064

Hi Christian,

I've gone back through the ticket in question and this is what was there:

add_filter("wpt_field_options", 'changenotset', 10, 3);

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->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
) );
if (empty($count)){
$count = 0;
}

return number_format($count, $format_decimals);
}
add_shortcode( 'count_numeric_custom_field', 'count_numeric_custom_field_func' );

changenotset only appears once.

#557257
#557288

Hi Christian,

That's a different set of code that works just fine. I managed to track down the original code that Francesco worked on here:

https://toolset.com/forums/topic/number-fields-sum-for-specific-post-types-and-fields/

add_filter("wpt_field_options", 'changenotset', 10, 3);

/* 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);
} */

function count_numeric_custom_field_func( $atts ) {
global $wpdb;
extract( shortcode_atts( array(
'field' => '',
'cpt' => '',
'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->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
) );
if (empty($count)){
$count = 0;
}

return number_format($count, $format_decimals);
}

add_shortcode( 'count_numeric_custom_field', 'count_numeric_custom_field_func' );

Thanks.

#557378

I do not understand, sorry. The "changenotset" function is not anywhere in this post:
https://toolset.com/forums/topic/number-fields-sum-for-specific-post-types-and-fields/

The "changenotset" reference is included in the code you provided above. What am I missing? Why is this line included in your code above?

add_filter("wpt_field_options", 'changenotset', 10, 3);
#557393

Hi Christian,

Yes, I see that now. I am not sure where the "changenotset" came from.

I am now using:

//Calculate membership fields
function count_numeric_custom_field_func( $atts ) {
global $wpdb;
extract( shortcode_atts( array(
'field' => '',
'cpt' => '',
'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->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
) );
if (empty($count)){
$count = 0;
}

return number_format($count, $format_decimals);
}

add_shortcode( 'count_numeric_custom_field', 'count_numeric_custom_field_func' );

It doesn't seem to mess with the form fields on the front end anymore, but what the function is meant to do doesn't seem to be working anymore.

#557403

what the function is meant to do doesn't seem to be working anymore.
Okay, I'll be glad to take a look, but I need a lot more information. Can you tell me:
- Where are you using this shortcode? Show me a screenshot of a full page showing how it's implemented.
- What values are you using as arguments for this shortcode?
- What do you mean by not working - is it showing the wrong information, or showing no information when it should be showing some information, or showing an error?
- What results do you expect to see?
- Share a screenshot of a custom post that includes the custom field you are trying to count, so I can see the values of the custom fields

Thanks!

#557407

Hi Christian,

I went back and took a closer look and saw that my CPT's in the shortcode were wrong.

This is what I have on the page in the backend:

Material Weight Checked-in

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

Material Weight Checked-out

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

Money Saved

$[count_numeric_custom_field field='wpcf-material-value' cpt='material-check-out']

And the live version is here:

hidden link

Thanks for your help!

#557408

Okay, I still need your help with some answers and screenshots:
- Where are you using this shortcode? Show me a screenshot of a full page showing how it's implemented. I need this from wp-admin
- What results do you expect to see here?
- Share screenshots of a few of the material-check-in custom posts in wp-admin that include the custom field you are trying to count, so I can see the values of the custom fields. Do the same for a few material-check-out posts. I need these from wp-admin

#557410
Screen Shot 2017-08-07 at 4.25.09 PM.png
Screen Shot 2017-08-07 at 4.24.18 PM.png
Screen Shot 2017-08-07 at 4.22.56 PM.png

Hi Christian,

Here are some screenshots.

Do you need more info?

Should the code be cleaned up, simplified?

#557419

1. What exactly do you expect to see instead of this?

Material Weight Checked-in
2.00 KG

Material Weight Checked-out
3.00 KG

Money Saved
$21.00

2. Why do you expect to see something different? Please explain specific reasons, because I'm not familiar with your custom posts or their custom field values.
3. Is there more than one published Material Check-in post? If so, show me some screenshots of those posts in wp-admin so I can see the values of their Material Weight Checked-in custom fields.
4. Is there more than one published Material Check-out post? If so, show me some screenshots of those posts in wp-admin so I can see the values of their Material Weight Checked-out custom fields.

Thank you.

#557451

Hi Christian,

Sorry, I guess I wasn't clear in my Reply #557407.

This is what I expect to see:

Material Weight Checked-in
2.00 KG

Material Weight Checked-out
3.00 KG

Money Saved
$21.00

All is functioning as it should.

Thanks for your help and sorry again for my miscommunication.

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