44

Anyone know of a way to deregister custom post types?

Is there an equivalent to register_post_type()?

  • 1
    ANY ONE ELSE SEARCHING THIS . When you register a custom post type nothing is being saved to the database . The posts table in the wp db is unstructured . Its only registering with the internals of the wp script that you can create an entry into the posts table with the type of what ever you created with register posts . Hope that helps some one else . – MageProspero Feb 3 '16 at 23:00
  • 2
    As of WordPress 4.5 there is function to do that unregister_post_type. See Unregister a post type using unregister_post_type – Dhinju Divakaran Jul 12 '16 at 4:54
38

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.

  • 2
    You're welcome.. :) If you're adding your own post types in code form, the easier solution would be to simply remove the function/code you run that adds them. Also note, removing post types does not flush them out from your posts table, so you may have redundanct entries in that table. – t31os Nov 9 '10 at 14:16
  • 2
    @anu: The only thing that stays "permanent" if you don't execute the register_post_type() on each page load (init) is probably the permalink structure (refresh it and it will be gone), and the posts in the database, which you can remove by doing DELETE FROM wp_posts WHERE post_type = '[your custom post type]'. – Jan Fabry Nov 9 '10 at 14:35
  • Interestingly, I did remove the register_post_type code for that custom_post_type, and I hadn't added any posts to that cpt, but it didn't remove the admin UI. – anu Nov 9 '10 at 14:36
  • Or course, now that I try this again, it works as you guys described - ie the admin UI for the new cpt has gone. Probably me being a muppet :) Useful info anyway, so thanks. – anu Nov 9 '10 at 14:37
  • 3
    Update in Feb 2016 - this function was merged in core for WP 4.5.0 (to be lauched in April). – Ricardo BRGWeb Feb 4 '16 at 19:43
37

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');
  • this need to be on higher priority then the one used to register the post type otherwise it will not work – Mark Kaplun Jul 12 '16 at 5:46
  • @Dhinju Divakaran it is not deleting the old post type from db which I am not using now. – Faiyaz Alam Feb 3 '17 at 5:48
  • its working, make sure you give high priority to this action compare to the action used to register the post type. – Ajith R Nair May 14 '17 at 21:23
  • 1
    It's worth mentioning that unregister_post_type() does not delete anything from the database. It only prevents the post type from being registered. – Dave Romsey Mar 3 '18 at 0:40
  • This worked great for me, but keep in mind that you'll need to inform WordPress of the change by re-saving your Permalink structure in the Settings -> Permalinks admin page. Otherwise, pages with conflicting permalinks don't work correctly. – MarkPraschan Feb 1 at 17:23
7

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');    
}
5

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

  • 2
    register_post_type() is not a function that lends itself nicely to hooking. The normal way to register a post type is to run the function directly in a function hooked on init. Removing that action may have undesired effects, as there will often be other functionality removed with that function. – John P Bloch Nov 9 '10 at 13:59
  • @John P Bloch yep, I am aware of that. Sometimes you end up rebuilding all of init to get it right, in the end it depends on specific code and its quality/sanity – Rarst Nov 9 '10 at 14:21
1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.