1

I need to restrict the button "EMPTY TRASH" and make it delete only the posts only if the logged in user is the creator.

I tried to use the wp_trash_post and i need to decide in the function if i need to continue to trash or not.

How do i tell WordPress NOT to empty the trash from specific posts?

3
  • Then how you will be able to trash the post ? Why don't you give author access to your users ?
    – Sumit
    Apr 20, 2016 at 10:12
  • Every user must be able to manage its own posts from specific post types. So i have managed to bullet proof all other operations (create,edit,view)....I am still having problem for only this action...to make it clearer, for example me pre_get_posts gets posts only if the author is the logged in user....So basically i need a "EMPTY MY OWN POSTS ONLY"....
    – e4rthdog
    Apr 20, 2016 at 10:17
  • I would also like an answer to this question. When using post_types for syncing data via an API it would be nice to not delete/trash the post, but just set a property 'deleted' or 'published=false' so I can send removed posts in the stream of recently changed items. Oct 20, 2016 at 23:28

1 Answer 1

0

It's possible to utilize the pre_delete_post filter to short-circuit the deleting of posts.

add_filter( 'pre_delete_post', 'wpse_224246_pre_delete_post', 10, 3 );
function wpse_224246_pre_delete_post( $delete, $post, $force_delete ) {
  //* Escape early if post isn't already trashed
  if( 'trash' !== $post->post_status ) {
    return $delete;
  }
  //* Go ahead with deleting the post if the current user is the post author 
  if( get_current_user_id() === $post->post_author ) {
    return $delete;
  }
  //* Returning any other value besides null will short circuit the deletion
  return true;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.