Skip Navigation

[Gelöst] Before_delete_post hook not working

This support ticket is created vor 6 Jahre, 8 Monate. 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
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10: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/Kolkata (GMT+05:30)

This topic contains 10 Antworten, has 2 Stimmen.

Last updated by marcoR-6 vor 6 Jahre, 8 Monate.

Assisted by: Minesh.

Author
Artikel
#566339

I'm trying to add a function when a post is delete.
I need to update a custom field value of a post when another post is deleted.
I've add in my function.php this function:


add_action( 'before_delete_post', 'myfunc' );
function myfunc( $postid ){
   $key_1_value = update_post_meta( 841, 'wpcf-priority', 3 );
}

But that code not works.

I can't be able to find the issue. Can you help me please?

M.

#566371

Minesh
Supporter

Languages: Englisch (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

Well - are you deleting posts from Trash? Could you please share more information from where you are trying to delete the posts - from backend (admin) or front-end?

#566385

Hi Minesh,
I delete posts form FRONT END using cred_delete_post_child button.

M.

#566410

Minesh
Supporter

Languages: Englisch (English )

Timezone: Asia/Kolkata (GMT+05:30)

Well - the hook before_delete_post will only work when you try to delete the post permanently I mean when you delete posts from Trash.

Please check the following note:

It's important to note the hook runs only when the WordPress user empties the Trash. If you're using this hook note that it will not fire if the user is deleting an Attachment, since attachments are force deleted, i.e., not sent to the Trash. Instead use the delete_post() hook.
#566420

I've tried to do that, but not working yet....

add_action( 'delete_post', 'myfunc', 10 );
function myfunc( $postid ){
   $key_1_value = update_post_meta( 841, 'wpcf-priority', 3 );
}

Can you provide me a working piece of code please?

Thanks,
M.

#566439

Minesh
Supporter

Languages: Englisch (English )

Timezone: Asia/Kolkata (GMT+05:30)

Well - you should use the hook 'post_updated' and check if post_status changed to trash , then run your code.

For example:

function check_values($post_ID, $post_after, $post_before){

          if($post_after->post_status == 'trash'){
                  $key_1_value = update_post_meta( 841, 'wpcf-priority', 3 );
          }


}
add_action( 'post_updated', 'check_values', 10, 3 ); 

More info:
=> https://codex.wordpress.org/Plugin_API/Action_Reference/post_updated

#566455

I've tried to add that piece of code, but nothing appened. I've tried alse to avoid if statement and do the update_post_meta function outside the if.

But the value priority of my post with ID 841 doesn't change...

#566457

I've also tried this hook:

add_action( 'trash_post', 'function_to_run_on_post_trash' );

from: https://codex.wordpress.org/Plugin_API/Action_Reference/trash_post

But I still have the issue..

#566458

Minesh
Supporter

Languages: Englisch (English )

Timezone: Asia/Kolkata (GMT+05:30)

*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.

I would additionally need your permission to de- and re-activate Plugins and the Theme, and to change configurations on the site. This is also a reason the backup is really important. If you agree to this, please use the form fields I have enabled below to provide temporary access details (wp-admin and FTP).

I have set the next reply to private which means only you and I have access to it.

#566759

Minesh
Supporter

Languages: Englisch (English )

Timezone: Asia/Kolkata (GMT+05:30)

Could you please check now. I've added the following code to your current child theme's functions.php file:

function check_values($post_ID, $post_after, $post_before){
 
          if($post_after->post_status == 'trash' and $post_after->post_type=='slide-ipad'){
                  $key_1_value = update_post_meta( 969, 'wpcf-ordine-ipad', 9 );
          }
 
 
}
add_action( 'post_updated', 'check_values', 10, 3 ); 

Now, I can see that when I've deleted one entry - the last entry textbox value updated and displays 9. You can see that here:
=> hidden link

#566789

Perfect! It works, thank you so much!
So the problem was the plugin to add php functions?

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