WordPress empowers developers to tailor their content structure using custom post types and taxonomies. With the help of register_post_type
and register_taxonomy
, you can extend WordPress beyond basic posts and pages. This guide will show you how to implement both.
Step 1: Create a Custom Post Type
We’ll begin by creating a post type called “Books”.
function register_books_post_type() { $labels = array( 'name' => 'Books', 'singular_name' => 'Book', 'menu_name' => 'Books', 'name_admin_bar' => 'Book', 'add_new' => 'Add New', 'add_new_item' => 'Add New Book', 'edit_item' => 'Edit Book', 'new_item' => 'New Book', 'view_item' => 'View Book', 'all_items' => 'All Books', 'search_items' => 'Search Books', 'not_found' => 'No books found', 'not_found_in_trash' => 'No books in Trash', ); $args = array( 'labels' => $labels, 'public' => true, 'show_ui' => true, 'show_in_menu' => true, 'rewrite' => array('slug' => 'books'), 'has_archive' => true, 'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'comments'), ); register_post_type('book', $args); } add_action('init', 'register_books_post_type');
Step 2: Create a Custom Taxonomy
Now, we’ll add a taxonomy named “Genres” for categorizing books.
function add_genres_taxonomy() { $labels = array( 'name' => 'Genres', 'singular_name' => 'Genre', 'search_items' => 'Search Genres', 'all_items' => 'All Genres', 'edit_item' => 'Edit Genre', 'update_item' => 'Update Genre', 'add_new_item' => 'Add New Genre', 'new_item_name' => 'New Genre Name', 'menu_name' => 'Genres', 'not_found' => 'No genres found.', ); $args = array( 'hierarchical' => true, 'labels' => $labels, 'show_ui' => true, 'show_admin_column' => true, 'rewrite' => array('slug' => 'genre'), ); register_taxonomy('genre', 'book', $args); } add_action('init', 'add_genres_taxonomy');
With these snippets, you’ve added a new “Books” post type and associated it with a “Genres” taxonomy. This allows you to manage book entries more efficiently and categorize them meaningfully.
Final Thoughts
Custom post types and taxonomies help you structure your WordPress site for better usability. Mastering register_post_type
and register_taxonomy
is key for custom development.
External Links & Further Reading
- register_post_type() – WP Developer Docs
- register_taxonomy() – WP Developer Docs
- WPBeginner Tutorial
- WordPress Codex: Post Types