Anyone know of a way to deregister custom post types?
Is there an equivalent to register_post_type()
?
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It only takes a minute to sign up.
Sign up to join this communityAnyone know of a way to deregister custom post types?
Is there an equivalent to register_post_type()
?
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');
unregister_post_type()
does not delete anything from the database. It only prevents the post type from being registered.
Mar 3, 2018 at 0:40
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.
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]'
.
Nov 9, 2010 at 14:35
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');
}
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).
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.
Nov 9, 2010 at 13:59
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.
unregister_post_type
. See Unregister a post type using unregister_post_type