Get rid of "trash can" for custom post type

I have a CPT foo. I want to totally disable the trash can feature, so that any time on the the CPTs is deleted, it is simply permanently removed. Not put in the trash can. How can I do this programatically?

Topic trash hooks custom-post-types Wordpress

Category Web


If you wanted to do this for all post types, you'd simply do

define('EMPTY_TRASH_DAYS', 0);

see https://codex.wordpress.org/Trash_status#EMPTY_TRASH_DAYS_option.

But since you only want to do this for a custom post type, you will need to hook into 'trashed_post' like this:

add_action('trashed_post', function( $post_id ){ 
    [...]

then check your trashed posts' post type:

    if ( get_post_type($post_id) === 'foo') {

and use wp_delete_post() with the 'force delete' parameter set to true on posts that turn out to be of your targeted post type:

        wp_delete_post( $post_id, true );
    }
});

I'd guess WordPress will then probably still show the 'moved into trash bin' message, even though it'll appear to the user as if there was no trash bin and the post will indeed already be deleted entirely. So you might have to change the $labels that are passed to your register_post_type() call if that's the case.

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.