Remove Custom Post Types from WordPress

Has your new WordPress theme left you with spurious and awkward custom post types that you're never going to use clogging up your WordPress admin panel menu? We show you how to remove them.

Introduction

You can spend hours looking for the perfect WordPress theme for your next project before making the purchasing decision. But you’re making that decision with a large part of equation hidden from you. How will your new theme interact with the WordPress admin panel? What editing tools, custom post types, widgets and page templates does the theme provide?

It’s a source of frustation shared with many WordPress users. You install your newly purchased theme with a small amount of trepidation, and if you’re lucky everything is wonderful in your WordPress world.  If you’re unlucky you’re left with spurious and awkward custom post types that you never use clogging up your WordPress admin panel menu. So how do we get rid of them?

WordPress provides a mechanism based on events and actions to integrate with its subsystems.  When used in conjunction with a child theme functions.php file we have the necessary tools to enable us to remove these redundant custom post types with ease.

Which custom post types do I need to remove?

You know which custom post types we want to remove, but you’ll need to know what they are called in your WordPress theme so that you can remove them.

So how do you do that?

It’s easy! Create the following PHP function in your child theme functions.php file.

Do not add this function on your live site, do it on a staging server or on your localhost server running the same main and child theme.

function list_post_type_event() {
	$args = array(
		'public'   => true,
		'_builtin' => false
	);
	$output = 'names';
	$operator = 'and';
	$post_types = get_post_types( $args, $output, $operator );

	var_dump($post_types);
}
add_action('init','list_post_type_event', 1000);

If you now view your WordPress site you’ll see a list of all of the public post types known to your WordPress installation. Hopefully it will be fairly evident to you which ones you need to remove.

array (size=5)
  'cpt_name_1' => string 'Custom Post Type 1' (length=18)
  'cpt_name_2' => string 'Custom Post Type 2' (length=18)
  'cpt_name_3' => string 'Custom Post Type 3' (length=18)
  'cpt_name_4' => string 'Custom Post Type 4' (length=18)

Now that you know the names of the custom post types you want to remove, you can delete that function from your child theme functions.php file.

How To Remove Custom Post Types

To remove the custom post types you no longer require simply add the following function to your child theme functions.php file.

function delete_post_type_event() {
	unregister_post_type( 'cpt_name_1' );
	unregister_post_type( 'cpt_name_3' );
}
add_action('init','delete_post_type_event', 1000);

If you now view your WordPress admin panel you’ll see that the custom post types you removed no longer appear in the admin menu!