How To Add Taxonomy To Custom Post Type
In order to start working with taxonomies, you need to register a custom post type first. Below is the reference that you can copy/paste. Make sure to tweak it according to your liking! If you need to create multiple custom post types, I highly suggest to declare a function to organize them.
register_post_type(
‘press’,
array(
‘labels’ => array(
‘name’ => ‘Press’,
‘singular_name’ => ‘Press’,
),
‘public’ => true,
‘publicly_queryable’ => true,
‘has_archive’ => false,
‘rewrite’ => array(‘slug’ => $data[‘press_slug’]),
‘supports’ => array(‘title’, ‘editor’, ‘excerpt’, ‘author’, ‘thumbnail’, ‘comments’, ‘revisions’, ‘custom-fields’, ‘page-attributes’),
‘can_export’ => true,
)
Things To Know For WordPress Taxonomy
Please keep in mind that you need to change your taxonomy, please replace the “press_categories” and “press_tags” to your preferred name. The second array should also match on your custom post type so ‘press’ word should be changed. Kindly check my video for further instruction.
To add more arrays on your taxonomies, check our WordPress official documentation
WordPress Taxonomy Code For Categories
register_taxonomy('press_category', 'press', array('hierarchical' => true, 'label' => 'Categories', 'query_var' => true, 'rewrite' => true));WordPress Taxonomy Code For Tags
register_taxonomy('press_tags', 'press', array('hierarchical' => false, 'label' => 'Tags', 'query_var' => true, 'rewrite' => true));