Add Default Taxonomies to a Custom Post Type

Topic: WordPress

Published on Updated

In WordPress, the basic taxonomies, categories and tags, are only set up to be used on the default post type. If you want to add them to a custom post type you’ve created, you can do so by adding category and/or post_tag to the taxonomies array element in the post type’s argument array.

Full Example

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
<?php

// Register Movie Post Type

function register_movie_post_type()
{
  $labels = array(
    'name' => __('Movies', 'movie'),
    'singular_name' => __('Movie', 'movie'),
    'add_new' => __('New Movie', 'movie'),
    'add_new_item' => __('Add New Movie', 'movie'),
    'edit_item' => __('Edit Movie', 'movie'),
    'new_item' => __('New Movie', 'movie'),
    'view_item' => __('View Movie', 'movie'),
    'search_items' => __('Search Movies', 'movie'),
    'not_found' =>  __('No Movies Found', 'movie'),
    'not_found_in_trash' => __('No Movies found in Trash', 'movie'),
  );

  $args = array(
    'labels' => $labels,
    'public' => true,
    'publicly_queryable' => true,
    'hierarchical' => false,
    'supports' => array(
      'title',
      'editor',
      'author',
      'excerpt',
      'custom-fields',
      'thumbnail',
      'revisions',
    ),
    'taxonomies' => array('category', 'post_tag'),
    'rewrite' => array('slug' => 'movie'),
    'show_in_rest' => true,
    'menu_icon' => 'dashicons-video-alt3',
  );

  register_post_type('movie', $args);
}

add_action('init', 'register_movie_post_type', 5);