3
votes

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?

2
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
Yeah, that did the trick thanks :D - Asta
I'll post as an answer so others can benefit. - disinfor
yeah ill mark it :D - Asta

2 Answers

2
votes

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');
2
votes

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);