unregister_taxonomy_for_object_type()WP 3.7.0

Unlinks the taxonomy from the specified post type (or other object).

This function can be used to remove a taxonomy from a post type after the post type has been registered (and the taxonomy has already been added to it).

The taxonomy must be registered at the time this function is triggered. Taxonomies are often registered on the init hook, and the function may be called earlier. For this reason, it may not work. Keep this in mind!

This function only breaks the link between the taxonomy and the post type. The taxonomy is not physically deleted. To delete the taxonomy (unregister it), use unregister_taxonomy().

1 time — 0.000001 sec (speed of light) | 50000 times — 0.02 sec (speed of light) | PHP 7.0.32, WP 5.1
Hooks from the function

Returns

true|false. True if the taxonomy was successfully unlinked.

Usage

unregister_taxonomy_for_object_type( $taxonomy, $object_type );
$taxonomy(string) (required)
The name of the taxonomy, specified as the first argument in register_taxonomy().
$object_type(string) (required)
The name of the post type, specified as the first parameter in register_post_type().

Examples

0

#1 Remove tags from use for blog posts

This example would decouple "Tags" from "Posts". I.e. the "Tags" item would disappear from the "Posts" menu, and the "Tags" metabox would disappear when editing posts. There will be no real removal of the "Tags" taxonomy.

add_action( 'init', 'wpkama_unregister_tags', 99 );

function wpkama_unregister_tags(){
	unregister_taxonomy_for_object_type( 'post_tag', 'post' );

	// The same can be done with "Categories"
	// unregister_taxonomy_for_object_type( 'category', 'post' );
}

This code will remove the "Tags" admin menu item, the "Tags" column when viewing the list of posts and the Tags metabox when editing a single post.

Notes

  • Global. WP_Taxonomy[]. $wp_taxonomies The registered taxonomies.

Changelog

Since 3.7.0 Introduced.

unregister_taxonomy_for_object_type() code WP 7.0

function unregister_taxonomy_for_object_type( $taxonomy, $object_type ) {
	global $wp_taxonomies;

	if ( ! isset( $wp_taxonomies[ $taxonomy ] ) ) {
		return false;
	}

	if ( ! get_post_type_object( $object_type ) ) {
		return false;
	}

	$key = array_search( $object_type, $wp_taxonomies[ $taxonomy ]->object_type, true );
	if ( false === $key ) {
		return false;
	}

	unset( $wp_taxonomies[ $taxonomy ]->object_type[ $key ] );

	/**
	 * Fires after a taxonomy is unregistered for an object type.
	 *
	 * @since 5.1.0
	 *
	 * @param string $taxonomy    Taxonomy name.
	 * @param string $object_type Name of the object type.
	 */
	do_action( 'unregistered_taxonomy_for_object_type', $taxonomy, $object_type );

	return true;
}