Skip to content Skip to sidebar Skip to footer
Reading Time: 5 minutes

If you’re working with wordpress sometimes you want to add custom post types with it’s tags and categories to add into admin panel.For a example, “News” custom post type.You can follow this tutorial guide to add unique custom post types with it’s tags and categories to admin panel.No errors,no tags and categories mixing with other post types.No usages of plugins.

Features

  • Unlimited Custom post types
  • No mixing with other post types
  • Creating unique tags and categories
  • No usages of plugins
  • Simple and efficient code
  • Customize display columns
  • Custom admin menu icon
  • Sort column by post name
  • Filter posts by categories

Now we’ll see how to add custom post type to wp admin panel.
-First you must go to the theme folder and open the functions.php file.If you’re using premium themes you can use custom-functions.php or theme-function.php.
-After that at the end of the file put the following piece of code.Here I’m using “News” custom post type.

Step-1

add_action( 'init', 'sh_create_news_post_type' );
function sh_create_news_post_type() {
$args = array(
'description' => 'News Post Type',
'show_ui' => true,
'menu_position' => 4,// places menu item directly below Posts. Ex: 4,5,6..etc
'menu_icon' => 'dashicons-format-aside', // change your menu icon from here.
'exclude_from_search' => true,
'labels' => array(
'name'=> 'News',
'singular_name' => 'News',
'add_new' => 'Add News',
'add_new_item' => 'Add News',
'edit' => 'Edit News',
'edit_item' => 'Edit News',
'new-item' => 'New News',
'view' => 'View News',
'view_item' => 'View News',
'search_items' => 'Search News',
'not_found' => 'No News Found',
'not_found_in_trash' => 'No News Found in Trash',
'parent' => 'Parent News'
),
'public' => true,
'capability_type' => 'post',
'hierarchical' => false,
'rewrite' => true,
'supports' => array('title', 'editor', 'excerpt', 'thumbnail', 'comments'),
'has_archive' => true
);
register_post_type( 'news' , $args ); // News Post type name here.
}

 

After add the above code you can see the changes as following.
custom post types
Further note:
If you want to add custom menu icon go to following link and select the icon.Then copy it’s name(Ex:-dashicons-format-aside) and put here.
Dashicons

Step-2

Main part is finished.Now we’ll add it’s categories.Note here,I’ll explain little bit mistake many user’s doing.
If you add following mentioned code, you can see categories mixing with other post type’s,default post’s categories.So DON’T USE Following code. ‘taxonomies’ => array(‘category’) ,// mixing with others.

$args = array(
'description' => 'News Post Type',
'show_ui' => true,
'menu_position' => 4,// places menu item directly below Posts. Ex: 4,5,6..etc
'menu_icon' => 'dashicons-format-aside', // change your menu icon from here.
'exclude_from_search' => true,
'labels' => array(
'name'=> 'News',
'singular_name' => 'News',
'add_new' => 'Add News',
'add_new_item' => 'Add News',
'edit' => 'Edit News',
'edit_item' => 'Edit News',
'new-item' => 'New News',
'view' => 'View News',
'view_item' => 'View News',
'search_items' => 'Search News',
'not_found' => 'No News Found',
'not_found_in_trash' => 'No News Found in Trash',
'parent' => 'Parent News'
),
'public' => true,
'capability_type' => 'post',
'hierarchical' => false,
'rewrite' => true,
'supports' => array('title', 'editor', 'excerpt', 'thumbnail', 'comments'),
'taxonomies' => array('category') ,// mixing with others
'has_archive' => true
);

 

This is the correct code.

add_action('init', 'sh_register_news_taxonomy_category');
function sh_register_news_taxonomy_category() {
register_taxonomy('news_category',
'news',
array (
'labels' => array (
'name' => 'News Categories',
'singular_name' => 'News Categories',
'search_items' => 'Search News Categories',
'popular_items' => 'Popular News Categories',
'all_items' => 'All News Categories',
'parent_item' => 'Parent News Category',
'parent_item_colon' => 'Parent News Category:',
'edit_item' => 'Edit News Category',
'update_item' => 'Update News Category',
'add_new_item' => 'Add News Category',
'new_item_name' => 'New Category',
),
'hierarchical' =>true,
'show_ui' => true,
'show_tagcloud' => true,
'rewrite' => false,
'public'=>true
)
);
}

 

 

Step-3

Now we have to do the customize columns as the following diagram.

WordPress create custom post types with tags and categories

 

Add the following code to functions.php file.

add_filter("manage_edit-news_columns", "sh_news_edit_columns");
function sh_news_edit_columns($columns){
$columns = array(
"cb" => "",
"photo" => __("Image"),
"title" => __("News"),
"news_category" => __("News Category"),
"date" => __("Date")
);
return $columns;
}
add_action("manage_news_posts_custom_column", "sh_news_custom_columns");
function sh_news_custom_columns($column){
global $post;
switch ($column){
case "photo":
if(has_post_thumbnail()) the_post_thumbnail(array(50,50));
break;
case "news_category":
echo get_the_term_list($post->ID, 'news_category', '', ', ','');
break;
}
}

 

 

Step-4

Filter news and sort by name.
To do that add this code.

if ( isset($_GET['post_type']) ) {
$post_type = $_GET['post_type'];
}else {
$post_type = '';
}
if ( $post_type == 'news' ) { // Your post Type name here.lower cose letter
add_action( 'restrict_manage_posts','sh_news_filter_list' );
add_filter( 'parse_query','sh_perform_filtering' );
}
function sh_news_filter_list() {
global $typenow, $wp_query;
if ($typenow=='news') { //// Your post Type name here.lower cose letter
wp_dropdown_categories(array(
'show_option_all' => 'Show All News Category',
'taxonomy' => 'news_category',
'name' => 'news_category',
'orderby' => 'name',
'selected' =>( isset( $wp_query->query['news_category'] ) ? $wp_query->query['news_category'] : '' ),
'hierarchical' => false,
'depth' => 3,
'show_count' => false,
'hide_empty' => true,
));
}
}
function sh_perform_filtering( $query ){
$qv = &$query->query_vars;
if (( $qv['news_category'] ) && is_numeric( $qv['news_category'] ) ) {
$term = get_term_by( 'id', $qv['news_category'], 'news_category' );
$qv['news_category'] = $term->slug;
}
}

 

 

Step-5

Adding Tags to Custom post type. You can add following code to add tags to News post types.No mixing with other post types.

add_action( 'init', 'sh_news_tag_taxonomies' ); //change order add_action( 'init', 'news_tag_taxonomies', 0 );
function sh_news_tag_taxonomies() {
// Add new taxonomy, NOT hierarchical (like tags)
$labels = array(
'name' => _x( 'News Tags', 'taxonomy general name' ),
'singular_name' => _x( 'Tag', 'taxonomy singular name' ),
'search_items' => __( 'Search Tags' ),
'popular_items' => __( 'Popular Tags' ),
'all_items' => __( 'All Tags' ),
'parent_item' => null,
'parent_item_colon' => null,
'edit_item' => __( 'Edit Tag' ),
'update_item' => __( 'Update Tag' ),
'add_new_item' => __( 'Add New Tag' ),
'new_item_name' => __( 'New Tag Name' ),
'separate_items_with_commas' => __( 'Separate tags with commas' ),
'add_or_remove_items' => __( 'Add or remove tags' ),
'choose_from_most_used' => __( 'Choose from the most used tags' ),
'menu_name' => __( 'News Tags' ),
);
register_taxonomy('tag','news',array( // post type name here
'hierarchical' => false,
'labels' => $labels,
'show_ui' => true,
'update_count_callback' => '_update_post_term_count',
'query_var' => true,
'rewrite' => array( 'slug' => 'tag' ),
));
}

 

Now you have implemented all codes and after that you can see custom post types with tags and categories,sorting,filtering functions.
WordPress create custom post types with tags and categories

 

Error fixing

May be when you visited to the category page you may not be seen the post related to that category. To fix this add the following code to function.php file.

add_filter('pre_get_posts', 'modify_pre_query_request');
function modify_pre_query_request($query){
    if ($query->is_main_query()){
        if ($query->is_tax){
            $post_type = get_query_var('post_type');
            if (!$post_type){
                $post_type = array( 'post', 'YOUR POST TYPE HERE' );
                $query->set('post_type', $post_type);
            }
        }
    }
}

 

Additional Notes

If you want to add more admin menu icons then add this code to functions.php file.

function add_menu_icons_styles(){

}
add_action( 'admin_head', 'add_menu_icons_styles' );

 

Follow my another post describing this.