Remove Wordpress Post Types

Unregister custom post types in WordPress Themes

I have a few WordPress themes that are just easy to work with. I know them inside and out and can spin up a site very quickly. However, these themes come with custom posts types I don’t need or want, and I like to keep the content management workflows as lean as possible.

To remove unwanted custom post types set by themes you simply unregister them post type.


//working with a theme that is easy to builid in but come with some posts
function custom_unregister_theme_post_types() {
global $wp_post_types;

if ( isset( $wp_post_types[“portfolio”] ) ) {
unset( $wp_post_types[ “portfolio” ] );
}

if ( isset( $wp_post_types[“testimonial”] ) ) {
unset( $wp_post_types[ “testimonial” ] );
}

}
add_action( ‘init’, ‘custom_unregister_theme_post_types’, 20 );

In this particular theme there are a number of custom post types. Chapter 12, “Two is Two Many,” of Understanding Software by Max Kanat-Alexander has always stuck in my mind. If I find myself cutting and pasting code there is I can probably find a more generic solution. He writes, “I do this as soon as I’m tempted to have two implementations of something.”

This function could very easily start to look like this:


//working with a theme that is easy to builid in but come with some posts
function custom_unregister_theme_post_types() {
global $wp_post_types;

if ( isset( $wp_post_types[“portfolio”] ) ) {
unset( $wp_post_types[ “portfolio” ] );
}

if ( isset( $wp_post_types[“testimonial”] ) ) {
unset( $wp_post_types[ “testimonial” ] );
}

if ( isset( $wp_post_types[“review”] ) ) {
unset( $wp_post_types[ “review” ] );
}

if ( isset( $wp_post_types[“blog”] ) ) {
unset( $wp_post_types[ “bog” ] );
}

}
add_action( ‘init’, ‘custom_unregister_theme_post_types’, 20 );

So you can begin to make this more generic by storing the post types in an array and making the rest of the function generic. PHP’s foreach construct provides an easy way to iterate over arrays.


//working with a theme that is easy to builid in but come with some posts
function custom_unregister_theme_post_types() {
global $wp_post_types;

$arr = array(‘portfolio’,’testimonial’, ‘course’);

foreach ($arr as $value) {

if ( isset( $wp_post_types[$value] ) ) {
unset( $wp_post_types[$value] );
}

}
add_action( ‘init’, ‘custom_unregister_theme_post_types’, 20 );

Leave a Reply

Your email address will not be published. Required fields are marked *