Skip Navigation

[Resolved] Deleting taxonomy terms from a post edited by CRED

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

Problem: I would like to automatically remove all "payment" taxonomy terms from a post when a User edits this post with CRED.

Solution: Use the cred_save_data hook and the wp_set_object_terms function to remove any terms from the 'payment' taxonomy associated with the post being edited.

add_action('cred_save_data', 'my_save_data_action_remove_options',10,2);
function my_save_data_action_remove_options($post_id, $form_data)
{
   if ($form_data['id']==2226){
       wp_set_object_terms( $post_id, null, 'payment' );
    }
}

Relevant Documentation:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data
https://codex.wordpress.org/Function_Reference/wp_set_object_terms

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

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)

Tagged: 

This topic contains 10 replies, has 2 voices.

Last updated by Bochnacki 6 years, 9 months ago.

Assisted by: Christian Cox.

Author
Posts
#557262

I have the ability to highlight posts.
This is done by matching the corresponding products with the CPT "Payment" terms.

add_action( 'woocommerce_order_status_completed', 'add_featured_option', 10, 1 );
function add_featured_option( $order_id ) {	
    $cred_form_id = get_post_meta($order_id, '_cred_form_id', true);
	$cred_form_ids = array(39);// here you can add more CRED form IDs    
	if(	in_array($cred_form_id, $cred_form_ids) ){
        $order = wc_get_order( $order_id );	
        $post_id = get_post_meta($order_id, '_cred_post_id', true); 
		$terms = array();
        foreach ( $order->get_items() as $item ) {
            $product_id = $item['product_id'];
			if($product_id == 1174){ // this is product ID of "home"
				$terms[] = 795; // this is term ID of "inhome"
			}
			if($product_id == 981){ // this is product ID of "featured"
				$terms[] = 796; // this is term ID of "featured"
			}
			if($product_id == 1175){ // this is product ID of "intop"
				$terms[] = 797; // this is term ID of "intop"
			}
        } 
		if(!empty($terms)){
			$term_taxonomy_ids = wp_set_object_terms( $post_id, $terms, 'payment' );
		}
	}
}

I also have CRED forms with which you can extend the post display period.
I want to make the post extension form remove highlights from his meta if they were added earlier.
I'm trying to do this in code:

add_action('cred_save_data', 'my_save_data_action_remove_options',10,2);
function my_save_data_action_remove_options($post_id, $form_data)
{
    if ($form_data['id']==2226){
        if (isset($_POST['payment']))
        {
            delete_post_meta($post_id, '__payment', $_POST['payment'], true);
        }
    }
}

Unfortunately this my code does not work. My PHP knowledge is very poor.

Can someone help me to do this correctly?

#557406

Okay I see in your add_featured_option function you are setting some terms in the "payment" taxonomy for the post created by CRED:

$term_taxonomy_ids = wp_set_object_terms( $post_id, $terms, 'payment' );

Then in your cred_save_data hook you are attempting to remove some postmeta values from the post edited by CRED:

delete_post_meta($post_id, '__payment', $_POST['payment'], true);

I'm not sure I understand why you are using delete_post_meta. Is some information in the postmeta table? If so, I need to see how it is added so I can determine what syntax should be used.

If you have not used update_post_meta or add_post_meta to save information in postmeta, you should not use delete_post_meta to remove these terms from a post. You should use the same function,
wp_set_object_terms. The documentation for this function is here:
https://codex.wordpress.org/Function_Reference/wp_set_object_terms

#557564

Thanks for the advice.
Unfortunately, or did it wrong:

add_action('cred_save_data', 'my_save_data_action_remove_options',10,2);
function my_save_data_action_remove_options($post_id, $form_data)
{
    if ($form_data['id']==2226){
        if (isset($_POST['payment']))
        {
			wp_set_object_terms( $terms, null, 'payment' );
        }
    }
}

or this method does not work.

#557876
wp_set_object_terms( $terms, null, 'payment' );

The documentation says that the 1st parameter should be the object ID - the post where you want to remove the terms. Change $terms to $post_id so you are passing the correct information into the wp_set_object_terms function. If this does not work, start adding error log statements to determine where the problem is.

add_action('cred_save_data', 'my_save_data_action_remove_options',10,2);
function my_save_data_action_remove_options($post_id, $form_data)
{
  error_log('action hook called');
    if ($form_data['id']==2226){
       error_log('inside if');
        if (isset($_POST['payment']))
        {
           error_log('payment post is set');
            wp_set_object_terms( $post_id, null, 'payment' );
        }
    }
}

Then you can read these messages in the error log. If you do not know how to access your error log, go in your wp-config.php file and look for define(‘WP_DEBUG’, false);. Change it to:

define('WP_DEBUG', true);

Then add these lines, just before it says 'stop editing here':

ini_set('log_errors',TRUE);
ini_set('error_reporting', E_ALL);
ini_set('error_log', dirname(__FILE__) . '/error_log.txt');

Then submit the CRED form again. This will create an error_log.txt file in your site's root directory. Please send me its contents. Once that is done, you can revert the updates you made to wp-config.php.

#557886

Here is the file "error_log.txt:

[08-Aug-2017 19:53:54 UTC] action hook called
[08-Aug-2017 19:54:17 UTC] PHP Warning:  Invalid argument supplied for foreach() in /home/betamat/public_html/auto-box/wp-content/plugins/woocommerce/includes/class-wc-order-item-product.php on line 169
[08-Aug-2017 19:54:18 UTC] PHP Notice:  Undefined index: order_on-hold in /home/betamat/public_html/auto-box/wp-content/plugins/cred-commerce/classes/Form_Handler.php on line 82
[08-Aug-2017 19:54:25 UTC] PHP Notice:  get_currentuserinfo is <strong>deprecated</strong> since version 4.5.0! Use wp_get_current_user() instead. in /home/betamat/public_html/auto-box/wp-includes/functions.php on line 3831
[08-Aug-2017 19:54:25 UTC] PHP Notice:  get_currentuserinfo is <strong>deprecated</strong> since version 4.5.0! Use wp_get_current_user() instead. in /home/betamat/public_html/auto-box/wp-includes/functions.php on line 3831
[08-Aug-2017 19:54:25 UTC] PHP Notice:  get_currentuserinfo is <strong>deprecated</strong> since version 4.5.0! Use wp_get_current_user() instead. in /home/betamat/public_html/auto-box/wp-includes/functions.php on line 3831
[08-Aug-2017 19:54:26 UTC] PHP Notice:  get_currentuserinfo is <strong>deprecated</strong> since version 4.5.0! Use wp_get_current_user() instead. in /home/betamat/public_html/auto-box/wp-includes/functions.php on line 3831
[08-Aug-2017 19:54:26 UTC] PHP Notice:  get_currentuserinfo is <strong>deprecated</strong> since version 4.5.0! Use wp_get_current_user() instead. in /home/betamat/public_html/auto-box/wp-includes/functions.php on line 3831
[08-Aug-2017 19:54:26 UTC] PHP Notice:  Trying to get property of non-object in /home/betamat/public_html/auto-box/wp-content/plugins/toolset-classifieds/inc/message_system.class.php on line 368
[08-Aug-2017 19:54:41 UTC] action hook called
[08-Aug-2017 19:54:41 UTC] inside if
[08-Aug-2017 19:54:56 UTC] PHP Warning:  Invalid argument supplied for foreach() in /home/betamat/public_html/auto-box/wp-content/plugins/woocommerce/includes/class-wc-order-item-product.php on line 169
[08-Aug-2017 19:54:57 UTC] PHP Notice:  Undefined index: order_on-hold in /home/betamat/public_html/auto-box/wp-content/plugins/cred-commerce/classes/Form_Handler.php on line 82
[08-Aug-2017 19:55:07 UTC] PHP Notice:  get_currentuserinfo is <strong>deprecated</strong> since version 4.5.0! Use wp_get_current_user() instead. in /home/betamat/public_html/auto-box/wp-includes/functions.php on line 3831
[08-Aug-2017 19:55:07 UTC] PHP Notice:  get_currentuserinfo is <strong>deprecated</strong> since version 4.5.0! Use wp_get_current_user() instead. in /home/betamat/public_html/auto-box/wp-includes/functions.php on line 3831
[08-Aug-2017 19:55:07 UTC] PHP Notice:  get_currentuserinfo is <strong>deprecated</strong> since version 4.5.0! Use wp_get_current_user() instead. in /home/betamat/public_html/auto-box/wp-includes/functions.php on line 3831
[08-Aug-2017 19:55:07 UTC] PHP Notice:  get_currentuserinfo is <strong>deprecated</strong> since version 4.5.0! Use wp_get_current_user() instead. in /home/betamat/public_html/auto-box/wp-includes/functions.php on line 3831
[08-Aug-2017 19:55:08 UTC] PHP Notice:  get_currentuserinfo is <strong>deprecated</strong> since version 4.5.0! Use wp_get_current_user() instead. in /home/betamat/public_html/auto-box/wp-includes/functions.php on line 3831
[08-Aug-2017 19:55:08 UTC] PHP Notice:  Trying to get property of non-object in /home/betamat/public_html/auto-box/wp-content/plugins/toolset-classifieds/inc/message_system.class.php on line 368
#557889

Notice that you see all the log messages except "payment post is set"? This means that the conditional just before the last log message was not true:

if (isset($_POST['payment']))

In plain language, this means that the form did not contain any value for an input named "payment". That could mean that the input field is not included in your form, or the input value is empty. Can you copy + paste the markup from your CRED form editor here? I need to see how you implemented the "payment" field.

#557905

Well... I did not think about it...
In CREDIT form there is nothing associated with "payment". There is only a button that leads to WooCommerce payment.

[credform class="cred-form cred-keep-original"]
  [cred_field field="form_submit" value="Extend"]
[/credform]

Even I have no idea how to insert it into the CRED form.

#557913

I see, in this case the payment isset conditional is probably not necessary. Is there any reason why submitting this form should not remove the terms? Anytime this form is submitted, all 'payment' taxonomy terms should be removed from the CRED post. Is that correct? This conditional can be removed, unless you disagree. The updated code, without any error logging:

add_action('cred_save_data', 'my_save_data_action_remove_options',10,2);
function my_save_data_action_remove_options($post_id, $form_data)
{
   if ($form_data['id']==2226){
       wp_set_object_terms( $post_id, null, 'payment' );
    }
}
#557930

It works great. Thank you very much.

By the way, I noticed another problem with the code that I showed in https://toolset.com/forums/topic/deleting-cpt-terms-from-meta-post/#post-557262 This code that matches the corresponding products to the CPT "Payment" terms.

If I have already added a "Payment" option and want to add another, then this one added earlier is deactivated. For example: I have added the "featured" option and added the "intop" option, the "featured" option disappears, no longer assigned to the post.
It should be possible to add all three options.

I'm trying to change that, but I'm not capable ...
Can you help me change that, or do I have to create a new topic?

#557934

Hi, I think this answer will be more than a sentence or two, so please create a new topic. We can help you remove all the terms, or add to the existing terms, or pick and choose terms to keep, however you would like. Thanks!

#557938

Ok.
Thank you once again.

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