I’m trying to remove a custom post type that was set via a different theme in WordPress, now all of those posts are assigned to a post_type
of portfolio
. After a lot of searching, I found the code below, however it doesn’t seem to work. I tried adding it both to the new theme and the old themes functions.php
.
I want to remove the post_type and have the posts categorized and displayed as a normal post. I think what I’m doing is correct, but can’t seem to get it to work – I’ve posted both the code for the custom post type and the code to unregister the posts assigned to it.
Code to uregister post type
if ( ! function_exists( 'unregister_post_type' ) ) :
function unregister_post_type() {
global $wp_post_types;
if ( isset( $wp_post_types[ 'portfolio' ] ) ) {
unset( $wp_post_types[ 'portfolio' ] );
return true;
}
return false;
}
endif;
add_action('init', 'unregister_post_type');
Code that registered the post type
register_post_type( 'portfolio',
array(
'labels' => array(
'name' => __('Portfolio Items'),
'singular_name' => __('Portfolio Item'),
'add_new_item' => __('Add New Portfolio Item'),
'edit_item' => __('Edit Portfolio Item'),
'new_item' => __('New Portfolio Item'),
'view_item' => __('View Portfolio Item'),
'search_items' => __('Search Portfolio Items'),
'not_found' => __('No portfolio items found'),
'not_found_in_trash' => __('No portfolio items found in Trash')
),
'public' => true,
'show_ui' => true,
'hierarchical' => false,
'menu_position' => 7,
//'rewrite' => array('slug' => 'portfolio'),
'rewrite' => true,
'_built_in' => false,
'taxonomies' => array( 'post_tag','category','portfolio_tag', 'portfolio_category', 'client'),
'supports' => array( 'title','editor','author','thumbnail','excerpt','trackbacks','custom-fields','comments','revisions')
)
);
I was able to remove it in WordPress 4.6.1 using this code:
Your code look good! But if you unregister the post_type… the posts in it go away… So don’t unregister it too soon. Before you unregister the post type, migrate the posts from the post_type to normal posts. This plugin is handy for that: https://wordpress.org/plugins/post-type-switcher/
But if you don’t want to migrate the video posts to the default posts… You’ll have to modify your loop to include those portfolio-type posts:
& don’t forget to visit the permalinks page when working with custom post types to get WordPress to recognize the changes you’ve made.
Try this code (with a large priority number)
Note: make sure that the registered post type name is
portfolio
orportfolios
(with s), register_post_type( $post_type, $args )UPDATED:
unset( $wp_post_types[ "portfolio" ] ); //UPDATED
Here is the code for unregistering custom post types in wordpress. Just remember that you need to clear your functions.php from function that registered your post type in first place.
If the post type refuses to unregister, and if the “register_post_type” function isn’t in functions.php, see if the registration is saved in a “must use plugin”. As a reminder I place this comment at the top of my functions.php