Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Unregisters a post type and removes the menu item
<?php
/*
* Usage for a custom post type named 'movies':
* unregister_post_type( 'movies' );
*
* Usage for the built in 'post' post type:
* unregister_post_type( 'post', 'edit.php' );
*/
function unregister_post_type( $post_type, $slug = '' ){
global $wp_post_types;
if ( isset( $wp_post_types[ $post_type ] ) ) {
unset( $wp_post_types[ $post_type ] );
$slug = ( !$slug ) ? 'edit.php?post_type=' . $post_type : $slug;
remove_menu_page( $slug );
}
}
?>
@donaldG

This comment has been minimized.

Show comment Hide comment
@donaldG

donaldG Aug 1, 2014

Was trying to get this to work, found virtually the same code following the answer here: http://wordpress.stackexchange.com/questions/3820/deregister-custom-post-types

I needed to use this in a Child Theme to unregister a Custom Post Type (not the slug) registered from a paid theme's plugin. I ended up removing the parameter from the function, setting $post_type inside the function equal to the name of the post type and then using the 'init' action. Couldn't get it to work without the action (could be b/c of the theme & the post type coming via plugin I suppose) so I have:

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

add_action('init', 'unregister_post_type');

Thanks for the provided code!

donaldG commented Aug 1, 2014

Was trying to get this to work, found virtually the same code following the answer here: http://wordpress.stackexchange.com/questions/3820/deregister-custom-post-types

I needed to use this in a Child Theme to unregister a Custom Post Type (not the slug) registered from a paid theme's plugin. I ended up removing the parameter from the function, setting $post_type inside the function equal to the name of the post type and then using the 'init' action. Couldn't get it to work without the action (could be b/c of the theme & the post type coming via plugin I suppose) so I have:

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

add_action('init', 'unregister_post_type');

Thanks for the provided code!

@dieppon

This comment has been minimized.

Show comment Hide comment
@dieppon

dieppon Jan 27, 2015

It did not work for me until gave it the priority '100':

add_action('init', 'unregister_post_type',100);

dieppon commented Jan 27, 2015

It did not work for me until gave it the priority '100':

add_action('init', 'unregister_post_type',100);
@tyrann0us

This comment has been minimized.

Show comment Hide comment
@tyrann0us

tyrann0us Jul 14, 2015

Priority of '11' worked for me (WordPress default is '10').

Priority of '11' worked for me (WordPress default is '10').

@Grafiker29

This comment has been minimized.

Show comment Hide comment
@Grafiker29

Grafiker29 Oct 27, 2015

Where exactly do you place this code? in functions.php? and then when the custom post types are deregistered does it have to stay in there?

Where exactly do you place this code? in functions.php? and then when the custom post types are deregistered does it have to stay in there?

@fgilio

This comment has been minimized.

Show comment Hide comment
@fgilio

fgilio Sep 28, 2016

@kjprince

This comment has been minimized.

Show comment Hide comment
@kjprince

kjprince Oct 7, 2016

@fgilio good find, just note that the function can't be used to remove default post types.

kjprince commented Oct 7, 2016

@fgilio good find, just note that the function can't be used to remove default post types.

@goodyis

This comment has been minimized.

Show comment Hide comment
@goodyis

goodyis Oct 30, 2017

None of that worked for me; but probably because the CPT's were from the theme it self. "Flora"

What did work: [placed within child theme --> functions.php]

function delete_post_type(){
    unregister_post_type( 'wyde_portfolio' );
    unregister_post_type('wyde_team_member');
    unregister_post_type('wyde_testimonial');
}
add_action('init','delete_post_type');

Then used a plugin to rid DB of the post's data.
CPT Cleanup

goodyis commented Oct 30, 2017

None of that worked for me; but probably because the CPT's were from the theme it self. "Flora"

What did work: [placed within child theme --> functions.php]

function delete_post_type(){
    unregister_post_type( 'wyde_portfolio' );
    unregister_post_type('wyde_team_member');
    unregister_post_type('wyde_testimonial');
}
add_action('init','delete_post_type');

Then used a plugin to rid DB of the post's data.
CPT Cleanup

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment