unregister_taxonomy_for_object_type( string $taxonomy, string $object_type )

Remove an already registered taxonomy from an object type.


Description Description


Parameters Parameters

$taxonomy

(string) (Required) Name of taxonomy object.

$object_type

(string) (Required) Name of the object type.


Top ↑

Return Return

(bool) True if successful, false if not.


Top ↑

Source Source

File: wp-includes/taxonomy.php

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

Top ↑

Changelog Changelog

Changelog
Version Description
3.7.0 Introduced.


Top ↑

User Contributed Notes User Contributed Notes

  1. Skip to note 1 content
    Contributed by Jonathan Goldford

    Below is an example of how you can entirely remove tags from use for blog posts. 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.

    function wpdocs_unregister_tags_for_posts() {
        unregister_taxonomy_for_object_type( 'post_tag', 'post' );
    }
    add_action( 'init', 'wpdocs_unregister_tags_for_posts' );
    
  2. Skip to note 2 content
    function wpdocs_unregister_tags_for_posts() {
        unregister_taxonomy_for_object_type( 'post_tag', 'post' );
    }
    add_action( 'init', 'wpdocs_unregister_tags_for_posts' );
    

    Needed to remove category from the default post but I get an “undefined offset: 2” error when I use the above code. Is it that this function is no longer in core or are there aspects to it that’re missing?

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