unregister_taxonomy_for_object_type()WP 3.7.0

Remove an already registered taxonomy from an object type.

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

Return

true|false. True if successful, false if not.

Usage

unregister_taxonomy_for_object_type( $taxonomy, $object_type );
$taxonomy(string) (required)
Name of taxonomy object.
$object_type(string) (required)
Name of the object 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 6.4.3

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;
}