2 mins read

Add A New Custom Post Type with a Custom Taxonomy (Categories) to WordPress Functions.php

This will create a custom post type call Puppies and will have a non-hierarchical like standard WordPress Posts with a custom taxonomy called Breeds that will act like Categories.

/*
* Creating a function to create our CPT
*/
  
function puppies_post_type() {
  
// // Set UI labels for Custom Post Type
    $labels = array(
        'name'                => _x( 'Puppies', 'Post Type General Name', 'puppies'),
        'singular_name'       => _x( 'Puppy', 'Post Type Singular Name', 'puppies'),
        'menu_name'           => __( 'Puppies', 'theme-name'),
        'parent_item_colon'   => __( 'Parent Puppy', 'theme-name'),
        'all_items'           => __( 'All Puppies', 'theme-name'),
        'view_item'           => __( 'View Puppy', 'theme-name'),
        'add_new_item'        => __( 'Add New Puppy', 'theme-name'),
        'add_new'             => __( 'Add New', 'theme-name'),
        'edit_item'           => __( 'Edit Puppy', 'theme-name'),
        'update_item'         => __( 'Update Puppy', 'theme-name'),
        'search_items'        => __( 'Search Puppy', 'theme-name'),
        'not_found'           => __( 'Not Found', 'theme-name'),
        'not_found_in_trash'  => __( 'Not found in Trash', 'theme-name'),
    );
      
// Set other options for Custom Post Type
      
    $args = array(
        'label'               => __( 'Puppies', 'theme-name'),
        'description'         => __( 'Puppies For Sale or Adoption', 'puppies'),
        'labels'              => $labels,
        // Features this CPT supports in Post Editor
        'supports'            => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'comments', 'revisions', 'custom-fields', ),
        // You can associate this CPT with a taxonomy or custom taxonomy. 
        'taxonomies'          => array( 'breeds' ),
        /* A hierarchical CPT is like Pages and can have
        * Parent and child items. A non-hierarchical CPT
        * is like Posts.
        */
        'hierarchical'        => false,
        'public'              => true,
        'show_ui'             => true,
        'show_in_menu'        => true,
        'show_in_nav_menus'   => true,
        'show_in_admin_bar'   => true,
        'menu_position'       => 5,
        'menu_icon'             => 'dashicons-pets',
        'can_export'          => true,
        'has_archive'         => true,
        'exclude_from_search' => false,
        'publicly_queryable'  => true,
        'capability_type'     => 'post',
        'show_in_rest' => false,
  
    );
      
    // Registering your Custom Post Type
    register_post_type( 'puppies', $args );
  
}
  
/* Hook into the 'init' action so that the function
* Containing our post type registration is not 
* unnecessarily executed. 
*/
  
add_action( 'init', 'puppies_post_type', 0 );


// END CUSTOM POST TYPE
// Register Custom Category

register_taxonomy('breeds', ['cpt_name'], [ 
        'label' => __('Breeds', 'puppies'),
        'hierarchical' => true,
        'rewrite' => ['slug' => 'breed'],
        'show_admin_column' => true,
        'show_in_rest' => true,
        'public'                     => true,
        'show_ui'                    => true,
        'show_in_nav_menus'          => true,
        'show_tagcloud'              => true,
        'meta_box_cb'           => 'cb_taxonomy_select_meta_box',
        'labels' => [
            'singular_name' => __('Breeds', 'puppies'),
            'all_items' => __('All Breedss', 'puppies'),
            'edit_item' => __('Edit Breeds', 'puppies'),
            'view_item' => __('View Breeds', 'puppies'),
            'update_item' => __('Update Breeds', 'puppies'),
            'add_new_item' => __('Add New Breeds', 'puppies'),
            'new_item_name' => __('New Breeds Name', 'puppies'),
            'search_items' => __('Search Breedss', 'puppies'),
            'parent_item' => __('Parent Breeds', 'puppies'),
            'parent_item_colon' => __('Parent Breeds:', 'puppies'),
            'not_found' => __('No Breedss found', 'puppies'),
        ]
    ]);
    register_taxonomy_for_object_type('breeds', 'puppies');

// End Custom Post Type Category

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.