Languages: English • 中文(简体) • 日本語 (Add your language)
This function adds or overwrites a taxonomy. It takes in a name, an object name that it affects, and an array of parameters. It does not return anything.
Care should be used in selecting a taxonomy name so that it does not conflict with other taxonomies, post types, and reserved WordPress public and private query variables. A complete list of those is described in the Reserved Terms section. In particular, capital letters should be avoided (This was allowed in 3.0, but not enforced until 3.1 with the "Cheatin'" error).
<?php register_taxonomy( $taxonomy, $object_type, $args ); ?>
Use the init action to call this function. Calling it outside of an action can lead to troubles. See #15568 for details.
Better be safe than sorry when registering custom taxonomies for custom post types. Use register_taxonomy_for_object_type() right after the function to interconnect them. Else you could run into minetraps where the post type isn't attached inside filter callback that run during parse_request
or pre_get_posts
.
_x( 'Post Tags', 'taxonomy general name' )
or _x( 'Categories', 'taxonomy general name' )
. When internationalizing this string, please use a gettext context matching your post type. Example: _x('Writers', 'taxonomy general name');
_x( 'Post Tag', 'taxonomy singular name' )
or _x( 'Category', 'taxonomy singular name' )
. When internationalizing this string, please use a gettext context matching your post type. Example: _x('Writer', 'taxonomy singular name');
__( 'All Tags' )
or __( 'All Categories' )
__( 'Edit Tag' )
or __( 'Edit Category' )
__( 'View Tag' )
or __( 'View Category' )
__( 'Update Tag' )
or __( 'Update Category' )
__( 'Add New Tag' )
or __( 'Add New Category' )
__( 'New Tag Name' )
or __( 'New Category Name' )
__( 'Parent Category' )
parent_item
, but with colon :
in the end null, __( 'Parent Category:' )
__( 'Search Tags' )
or __( 'Search Categories' )
__( 'Popular Tags' )
or null__( 'Separate tags with commas' )
, or null__( 'Add or remove tags' )
or null__( 'Choose from the most used tags' )
or null__( 'No tags found.' )
or __( 'No categories found.' )
__( '← Back to tags' )
or __( '← Back to categories' )
Note: Defaults to the categories meta box (post_categories_meta_box() in meta-boxes.php) for hierarchical taxonomies and the tags meta box (post_tags_meta_box()) for non-hierarchical taxonomies. No meta box is shown if set to false.
Note: Hierarchical taxonomies will have a list with checkboxes to select an existing category in the taxonomy admin box on the post edit page (like default post categories). Non-hierarchical taxonomies will just have an empty text field to type-in taxonomy terms to associate with the post (like default post tags).
Note: While the default is '', when actually performing the count update in wp_update_term_count_now(), if the taxonomy is only attached to post types (as opposed to other WordPress objects, like user), the built-in _update_post_term_count() function will be used to count only published posts associated with that term, otherwise _update_generic_term_count() will be used instead, that does no such checking.
This is significant in the case of attachments. Because an attachment is a type of post, the default _update_post_term_count() will be used. However, this may be undesirable, because this will only count attachments that are actually attached to another post (like when you insert an image into a post). This means that attachments that you simply upload to WordPress using the Media Library, but do not actually attach to another post will not be counted. If your intention behind associating a taxonomy with attachments was to leverage the Media Library as a sort of Document Management solution, you are probably more interested in the counts of unattached Media items, than in those attached to posts. In this case, you should force the use of _update_generic_term_count() by setting '_update_generic_term_count' as the value for update_count_callback.
Another important consideration is that _update_post_term_count() only counts published posts. If you are using custom statuses, or using custom post types where being published is not necessarily a consideration for being counted in the term count, then you will need to provide your own callback that doesn't include the post_status portion of the where clause.
Note: The query_var is used for direct queries through WP_Query like new WP_Query(array('people'=>$person_name))
and URL queries like /?people=$person_name
. Setting query_var to false will disable these methods, but you can still fetch posts with an explicit WP_Query taxonomy query like WP_Query(array('taxonomy'=>'people', 'term'=>$person_name))
.
Note: You may need to flush the rewrite rules after changing this. You can do it manually by going to the Permalink Settings page and re-saving the rules -- you don't need to change them -- or by calling $wp_rewrite->flush_rules(). You should only flush the rules once after the taxonomy has been created, not every time the plugin/theme loads.
An example of registering a two taxonomies, genres and writers, for the post type called "book" (uses Version 3.1 arguments):
Note: You can define custom taxonomies in a themes's functions.php
template file:
<?php // hook into the init action and call create_book_taxonomies when it fires add_action( 'init', 'create_book_taxonomies', 0 ); // create two taxonomies, genres and writers for the post type "book" function create_book_taxonomies() { // Add new taxonomy, make it hierarchical (like categories) $labels = array( 'name' => _x( 'Genres', 'taxonomy general name', 'textdomain' ), 'singular_name' => _x( 'Genre', 'taxonomy singular name', 'textdomain' ), 'search_items' => __( 'Search Genres', 'textdomain' ), 'all_items' => __( 'All Genres', 'textdomain' ), 'parent_item' => __( 'Parent Genre', 'textdomain' ), 'parent_item_colon' => __( 'Parent Genre:', 'textdomain' ), 'edit_item' => __( 'Edit Genre', 'textdomain' ), 'update_item' => __( 'Update Genre', 'textdomain' ), 'add_new_item' => __( 'Add New Genre', 'textdomain' ), 'new_item_name' => __( 'New Genre Name', 'textdomain' ), 'menu_name' => __( 'Genre', 'textdomain' ), ); $args = array( 'hierarchical' => true, 'labels' => $labels, 'show_ui' => true, 'show_admin_column' => true, 'query_var' => true, 'rewrite' => array( 'slug' => 'genre' ), ); register_taxonomy( 'genre', array( 'book' ), $args ); // Add new taxonomy, NOT hierarchical (like tags) $labels = array( 'name' => _x( 'Writers', 'taxonomy general name', 'textdomain' ), 'singular_name' => _x( 'Writer', 'taxonomy singular name', 'textdomain' ), 'search_items' => __( 'Search Writers', 'textdomain' ), 'popular_items' => __( 'Popular Writers', 'textdomain' ), 'all_items' => __( 'All Writers', 'textdomain' ), 'parent_item' => null, 'parent_item_colon' => null, 'edit_item' => __( 'Edit Writer', 'textdomain' ), 'update_item' => __( 'Update Writer', 'textdomain' ), 'add_new_item' => __( 'Add New Writer', 'textdomain' ), 'new_item_name' => __( 'New Writer Name', 'textdomain' ), 'separate_items_with_commas' => __( 'Separate writers with commas', 'textdomain' ), 'add_or_remove_items' => __( 'Add or remove writers', 'textdomain' ), 'choose_from_most_used' => __( 'Choose from the most used writers', 'textdomain' ), 'not_found' => __( 'No writers found.', 'textdomain' ), 'menu_name' => __( 'Writers', 'textdomain' ), ); $args = array( 'hierarchical' => false, 'labels' => $labels, 'show_ui' => true, 'show_admin_column' => true, 'update_count_callback' => '_update_post_term_count', 'query_var' => true, 'rewrite' => array( 'slug' => 'writer' ), ); register_taxonomy( 'writer', 'book', $args ); } ?>
<?php add_action( 'init', 'create_book_tax' ); function create_book_tax() { register_taxonomy( 'genre', 'book', array( 'label' => __( 'Genre' ), 'rewrite' => array( 'slug' => 'genre' ), 'hierarchical' => true, ) ); } ?>
Note: If you want to ensure that your custom taxonomy behaves like a tag, you must add the option 'update_count_callback' => '_update_post_term_count'. Not doing so will result in multiple comma-separated items added at once being saved as a single value, not as separate values. This can cause undue stress when using get_the_term_list and other term display functions.
If you do not want your taxonomy to be exposed publicly, you can use the 'public' and 'rewrite' parameters to suppress it. It will be available to use internally by your plugin or theme, but will not generate a url of it's own.
<?php add_action( 'init', 'create_private_book_tax' ); function create_private_book_tax() { register_taxonomy( 'genre', 'book', array( 'label' => __( 'Genre' ), 'public' => false, 'rewrite' => false, 'hierarchical' => true, ) ); } ?>
Avoiding the following reserved terms is particularly important if you are passing the term through the $_GET or $_POST array. Doing so can cause WordPress to respond with a 404 error without any other hint or explanation.
register_taxonomy() is located in wp-includes/taxonomy.php
.