Created

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

Unregisters a post type and removes the menu item

View gist:769160
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
<?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 );
}
}
?>

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!

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

add_action('init', 'unregister_post_type',100);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.