3

So I want my posts in my custom post type deleted permanently instead of moving to trash first. so I found this code online that is supposed to do the trick. But I'm not able to get it to work somehow.

The code that I have is as follows.

function directory_skip_trash($post_id) {
    if (get_post_type($post_id) == 'directory') {
        // Force delete
        wp_delete_post( $post_id, true );
    }
}
add_action('wp_trash_post', 'directory_skip_trash');

The rest of the code of my custom post type itself u can find in this post that I've made earlier.

I'm probably missing out on something pretty simple.

UPDATE

so now it got it to kinda work. It does actually delete the post but I get this error. enter image description here after the error, if I return to the post page the post is gone and not in the trash anyone that might have a solution for it to just remove and not give this message?

Asta
  • 121
  • 1
  • 14
  • 1
    Instead of using `wp_trash_post` as your hook, try `trashed_post`. The problem is `wp_trash_post` happens BEFORE the post is moved to the trash. So you are deleting the post and then running on the hook, but since the post doesn't exist anymore, you're getting an error. – disinfor Feb 27 '20 at 15:17
  • Yeah, that did the trick thanks :D – Asta Feb 27 '20 at 15:20
  • 1
    I'll post as an answer so others can benefit. – disinfor Feb 27 '20 at 15:21
  • 1
    yeah ill mark it :D – Asta Feb 27 '20 at 15:21

2 Answers2

3

Using the correct members post type:

You need to use the trashed_post hook. wp_trash_post happens BEFORE the post is trashed, so you're getting an error because the post didn't exist anymore.

function members_skip_trash($post_id) {
    if (get_post_type($post_id) == 'members') { // <-- members type posts
        // Force delete
        wp_delete_post( $post_id, true );
    }
}
add_action('trashed_post', 'members_skip_trash');
disinfor
  • 10,865
  • 2
  • 33
  • 44
2

In your other post, it said your post type is called 'members'. You're instead checking for 'directory' type posts. Change the if-condition like so to actually affect the correct post type:

function members_skip_trash($post_id) {
    if (get_post_type($post_id) == 'members') { // <-- members type posts
        // Force delete
        remove_all_actions('wp_trash_post');
        wp_delete_post( $post_id, true );
    }
}
add_action('trashed_post', 'members_skip_trash', 1);
Damocles
  • 1,287
  • 1
  • 7
  • 9
  • so I've done that now it kinda works. I get an error like: Error while moving to trash. but when I go back to the page it did delete it. but the thing is I have WP_DEBUG on in wp-config but I don't get an error line only a Wordpress error – Asta Feb 27 '20 at 14:59
  • Is `define( 'WP_DEBUG_LOG', true );` also on? Maybe something will show up in the log file. I currently don't see any obvious error here and since it didn't produce any error before, I doubt the error happens anywhere in this function. Probably, this is causing some side effects with other hooks trying to access the trashed post.. – Damocles Feb 27 '20 at 15:10
  • @Damocles check my answer. `wp_trash_post` is the wrong hook. – disinfor Feb 27 '20 at 15:24
  • Your code looks awfully similar to mine.. especially with that comment. – Damocles Feb 27 '20 at 15:28
  • Yeah, I used the correct hook. I read your answer, realized that you corrected an error in OPs original function from a previous question. And I posted the correct answer in my comment before I posted my answer. – disinfor Feb 27 '20 at 15:33