Custom taxonomies allow you to structure and organize content beyond the defaults. They are useful when:
- You have custom post types (e.g., products, portfolios, events).
- You need specific groupings that don’t fit the default categories/tags.
- You want better navigation and filtering for users.
Example: For a real estate site:
- Custom post type: Properties
- Taxonomies:
- Locations (e.g., “New York,” “San Francisco”)
- Property Types (e.g., “Apartment,” “Condo,” “House”)
In register custom post types with taxonomies in WordPress, you can use the register_post_type
and register_taxonomy
functions. Step-by-step guide to set up both a custom post type and its associated taxonomies:
-
Add the code to your theme’s
functions.php
file or use a custom plugin if you want to keep it separate from the theme files. -
Define the custom post type using
register_post_type
. -
Define the taxonomies with
register_taxonomy
.
Explanation
-
Custom Post Type
book
: This is a simple custom post type for books with basic options enabled. -
Taxonomy
genre
: A hierarchical taxonomy, similar to categories, allowing users to assign genres to each book. -
Taxonomy
author
: A non-hierarchical taxonomy, similar to tags, allowing users to assign authors to each book.
After adding this code, the “Books” post type, along with the “Genre” and “Author” taxonomies, will be available in the WordPress admin menu. This setup allows you to create book posts and categorize them by genre and author.
function register_custom_post_types_with_taxonomies() {
// Register Custom Post Type: Books
$post_type_labels = array(
'name' => __('Books', 'text_domain'),
'singular_name' => __('Book', 'text_domain'),
'menu_name' => __('Books', 'text_domain'),
'name_admin_bar' => __('Book', 'text_domain'),
'add_new_item' => __('Add New Book', 'text_domain'),
'new_item' => __('New Book', 'text_domain'),
'edit_item' => __('Edit Book', 'text_domain'),
'view_item' => __('View Book', 'text_domain'),
'all_items' => __('All Books', 'text_domain'),
'search_items' => __('Search Books', 'text_domain'),
'not_found' => __('No books found', 'text_domain'),
'not_found_in_trash' => __('No books found in Trash', 'text_domain')
);
$post_type_args = array(
'labels' => $post_type_labels,
'public' => true,
'show_in_menu' => true,
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'comments'),
'has_archive' => true,
'rewrite' => array('slug' => 'books'),
);
register_post_type('book', $post_type_args);
// Register Taxonomy: Genre
$genre_labels = array(
'name' => __('Genres', 'text_domain'),
'singular_name' => __('Genre', 'text_domain'),
'search_items' => __('Search Genres', 'text_domain'),
'all_items' => __('All Genres', 'text_domain'),
'edit_item' => __('Edit Genre', 'text_domain'),
'update_item' => __('Update Genre', 'text_domain'),
'add_new_item' => __('Add New Genre', 'text_domain'),
'new_item_name' => __('New Genre Name', 'text_domain'),
'menu_name' => __('Genres', 'text_domain'),
);
$genre_args = array(
'hierarchical' => true,
'labels' => $genre_labels,
'show_ui' => true,
'show_admin_column' => true,
'rewrite' => array('slug' => 'genre'),
);
register_taxonomy('genre', array('book'), $genre_args);
// Register Taxonomy: Author
$author_labels = array(
'name' => __('Authors', 'text_domain'),
'singular_name' => __('Author', 'text_domain'),
'search_items' => __('Search Authors', 'text_domain'),
'all_items' => __('All Authors', 'text_domain'),
'edit_item' => __('Edit Author', 'text_domain'),
'update_item' => __('Update Author', 'text_domain'),
'add_new_item' => __('Add New Author', 'text_domain'),
'new_item_name' => __('New Author Name', 'text_domain'),
'menu_name' => __('Authors', 'text_domain'),
);
$author_args = array(
'hierarchical' => false,
'labels' => $author_labels,
'show_ui' => true,
'show_admin_column' => true,
'rewrite' => array('slug' => 'author'),
);
register_taxonomy('author', array('book'), $author_args);
}
add_action('init', 'register_custom_post_types_with_taxonomies');