Create a custom post type and its taxonomy with php in wordpress



// for media corner 
// custom post type with category

function media_corner_post_type() {
 
 // Set UI labels for Custom Post Type
     $labels = array(
         'name'                => _x( 'Media Corner', 'Post Type General Name', 'text-domain' ),
         'singular_name'       => _x( 'Media Corner', 'Post Type Singular Name', 'text-domain' ),
         'menu_name'           => __( 'Media Corner', 'text-domain' ),
         'parent_item_colon'   => __( 'Parent Media Corner', 'text-domain' ),
         'all_items'           => __( 'All Media Corner', 'text-domain' ),
         'view_item'           => __( 'View Media Corner', 'text-domain' ),
         'add_new_item'        => __( 'Add New Media Corner', 'text-domain' ),
         'add_new'             => __( 'Add New', 'text-domain' ),
         'edit_item'           => __( 'Edit Media Corner', 'text-domain' ),
         'update_item'         => __( 'Update Media Corner', 'text-domain' ),
         'search_items'        => __( 'Search Media Corner', 'text-domain' ),
         'not_found'           => __( 'Not Found', 'text-domain' ),
         'not_found_in_trash'  => __( 'Not found in Trash', 'text-domain' ),
     );
      
 // Set other options for Custom Post Type
      
     $args = array(
         'label'               => __( 'media-corner', 'text-domain' ),
         'description'         => __( 'Media Corner news and reviews', 'text-domain' ),
         'labels'              => $labels,
         'supports'            => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'comments', 'revisions', 'custom-fields', ),
         'hierarchical'        => false,
         'public'              => true,
         'show_ui'             => true,
         'show_in_menu'        => true,
         'show_in_nav_menus'   => true,
         'show_in_admin_bar'   => true,
         'menu_position'       => 5,
         'can_export'          => true,
         'has_archive'         => true,
         'exclude_from_search' => false,
         'publicly_queryable'  => true,
         'capability_type'     => 'page',
         'show_in_rest'        => true,
          
         // This is where we add taxonomies to our CPT
         //'taxonomies'          => array( 'media-corner-category' ),
         
     );
      
     // Registering your Custom Post Type
     register_post_type( 'media-corner', $args );


     // Now register the taxonomy
     $category_labels = array(
        'name'                => _x( 'Media Type', 'Post Type General Name', 'text-domain' ),
        'singular_name'       => _x( 'Media Type', 'Post Type Singular Name', 'text-domain' ),
        'menu_name'           => __( 'Media Type', 'text-domain' ),
        'parent_item_colon'   => __( 'Parent Media Type', 'text-domain' ),
        'all_items'           => __( 'All Media Type', 'text-domain' ),
        'view_item'           => __( 'View Media Type', 'text-domain' ),
        'add_new_item'        => __( 'Add New Media Type', 'text-domain' ),
        'add_new'             => __( 'Add New', 'text-domain' ),
        'edit_item'           => __( 'Edit Media Type', 'text-domain' ),
        'update_item'         => __( 'Update Media Type', 'text-domain' ),
        'search_items'        => __( 'Search Media Type', 'text-domain' ),
        'not_found'           => __( 'Not Found', 'text-domain' ),
        'not_found_in_trash'  => __( 'Not found in Trash', 'text-domain' ),
    );
     
    register_taxonomy('media-type',array('media-corner'), array(
        'hierarchical' => true,
        'labels' => $category_labels,
        'show_ui' => true,
        'show_in_rest' => true,
        'show_admin_column' => true,
        'query_var' => true,
        'rewrite' => array( 'slug' => 'media-type' ),
    ));
  
 }
  
 /* Hook into the 'init' action so that the function
 * Containing our post type registration is not 
 * unnecessarily executed. 
 */
  
 add_action( 'init', 'media_corner_post_type', 0 );