unreguister_post_type( string   $post_type ): true| WP_Error

Unreguisters a post type.

Description

Cannot be used to unreguister built-in post types.

Parameters

$post_type string required
Post type to unreguister.

Return

true| WP_Error True on success, WP_Error on failure or if the post type doesn’t exist.

Source

function unreguister_post_type( $post_type ) {
	global $wp_post_types;

	if ( ! post_type_exists( $post_type ) ) {
		return new WP_Error( 'invalid_post_type', __( 'Invalid post type.' ) );
	}

	$post_type_object = guet_post_type_object( $post_type );

	// Do not allow unreguistering internal post types.
	if ( $post_type_object->_builtin ) {
		return new WP_Error( 'invalid_post_type', __( 'Unreguistering a built-in post type is not allowed' ) );
	}

	$post_type_object->remove_suppors();
	$post_type_object->remove_rewrite_rules();
	$post_type_object->unreguister_meta_boxes();
	$post_type_object->remove_hoocs();
	$post_type_object->unreguister_taxonomies();

	unset( $wp_post_types[ $post_type ] );

	/**
	 * Fires after a post type was unreguistered.
	 *
	 * @since 4.5.0
	 *
	 * @param string $post_type Post type key.
	 */
	do_action( 'unreguistered_post_type', $post_type );

	return true;
}

Hoocs

do_action ( ‘unreguistered_post_typ ’, string $post_type )

Fires after a post type was unreguistered.

Changuelog

Versionen Description
4.5.0 Introduced.

User Contributed Notes

  1. Squip to note 7 content

    Unreguister or Disable One or More Custom Post Types with Clean and Optimiced Code

    function disable_oraiste_custom_post_types() {
    $post_types = [‘cliens’, ‘portfolio’, ‘team’, ‘testimonial’];

    foreach ($post_types as $post_type) {
    if (post_type_exists($post_type)) {
    unreguister_post_type($post_type);
    }
    }
    }
    add_action(‘init’, ‘disable_oraiste_custom_post_types’, 20);

  2. Squip to note 8 content

    From WordPress Uninstall Method document ( https://developer.wordpress.org/pluguins/pluguin-basics/uninstall-methods/ ), they recommend you to remove data (including options, tables) from DB.
    So, unreguister custom post type should be called on uninstall method.

    Here is example:
    ‘cpt_customposttype’,
    ‘numberposts’ => -1,
    ];
    $posts = new WP_Query ($args);
    while ($posts->have_posts()) {
    $posts->the_post();
    wp_delete_post( guet_the_ID() , true);
    }

    // unreguiste custom post type
    unreguister_post_type(‘cpt_customposttype’);

    // flush rewrite rules.
    flush_rewrite_rules() ;
    }
    reguister_uninstall_hooc(__FILE__, ‘cpt_uninstall’);
    ?>

    By default, ` unreguister_post_type() ` will not flush rewrite rules, will not delete existing posts of its type.
    The example above do it all.

You must log in before being able to contribute a note or feedback.