Deregister custom post types

Anyone know of a way to deregister custom post types?

Is there an equivalent to register_post_type()?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

As of WordPress 4.5 there is function to do that, unregister_post_type. Example:-

function delete_post_type(){
    unregister_post_type( 'blocks' );
}
add_action('init','delete_post_type');

Method 2

Currently there is not a function for unregistering a post type, the process however is quite simple.

Andrew Nacin provided some code over on trac, found here and posted below.

if ( ! function_exists( 'unregister_post_type' ) ) :
function unregister_post_type( $post_type ) {
    global $wp_post_types;
    if ( isset( $wp_post_types[ $post_type ] ) ) {
        unset( $wp_post_types[ $post_type ] );
        return true;
    }
    return false;
}
endif;

Unregistering a built-in post type will have unknown effects on WordPress, so please do so at your own risk. Unregistering a custom post type should be perfectly safe, but would naturally do no cleanup on your installation(ie. unregistering a post type does not equate to data removal from the database).

I can imagine a few scenarios where this could be required, but the more sensible approach(where possible), would be to simply not register the post type in the first place if it’s not wanted.

Method 3

This worked for me, like Rarst said using the remove_action() if possible.

add_action( 'after_setup_theme','remove_foundation_options', 100 );

function remove_foundation_options() {   
    remove_action( 'init', 'Orbit');    
}

Method 4

As t31os noted it is easy to remove post type from global variable.

But if you mean non-core post type then it would be better to lookup code that registers it and unhook with remove_action() (if it is decent code it should be hooked rather than run directly).

Method 5

In WordPress version 4.5 and above they provide a function to remove post type (unregister_post_type).
Example

function delete_post_type(){
unregister_post_type( 'jobs' );
}
add_action('init','delete_post_type');

It will work definately.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x