How to create New Custom Post Type and Custom Taxonomy using function?

banner-image

Sometimes we need to create Custom Post Types and Custom Taxonomy for our WordPress sites.

For that, some plugins are available to do that but if we need to create that using custom functions, we can do that manually.

Assume, we have to create a post-type name “News” and have to create a taxonomy/category under that post-type name “News Category“.

For that, first, we have to register the post-type.

Please use the below code on your active theme’s functions.php file.

//For Post type
function my_custom_post_news() {
  $labels = array(
    'name'               => _x( 'News', 'post type general name' ),
    'singular_name'      => _x( 'News', 'post type singular name' ),
    'add_new'            => _x( 'Add New', 'book' ),
    'add_new_item'       => __( 'Add New News' ),
    'edit_item'          => __( 'Edit News' ),
    'new_item'           => __( 'New News' ),
    'all_items'          => __( 'All News' ),
    'view_item'          => __( 'View News' ),
    'search_items'       => __( 'Search News' ),
    'not_found'          => __( 'No News found' ),
    'not_found_in_trash' => __( 'No News found in the Trash' ), 
    'parent_item_colon'  => '',
    'menu_name'          => 'News'
  );
  $args = array(
    'labels'        => $labels,
    'description'   => 'Holds our News and news specific data',
    'public'        => true,
    'menu_position' => 5,
    'supports'      => array( 'title', 'editor', 'thumbnail', 'excerpt', 'comments' ),
    'has_archive'   => true, //make it false to deactivate archive
  );
  register_post_type( 'news', $args ); 
}
add_action( 'init', 'my_custom_post_news' );

 

After this, you will see a new post-type on the WordPress admin section.

Now, we have to create a category/taxonomy of that particular post-type name “News Category”.

For that, use the below function on your active themes funnctions.php again.

//For Taxonomies
add_action( 'init', 'create_news_hierarchical_taxonomy', 0 );
 
function create_news_hierarchical_taxonomy() {
  $labels = array(
    'name' => _x( 'News Category', 'taxonomy general name' ),
    'singular_name' => _x( 'Topic', 'taxonomy singular name' ),
    'search_items' =>  __( 'Search News Category' ),
    'all_items' => __( 'All News Category' ),
    'parent_item' => __( 'Parent Topic' ),
    'parent_item_colon' => __( 'Parent Topic:' ),
    'edit_item' => __( 'Edit Topic' ), 
    'update_item' => __( 'Update Topic' ),
    'add_new_item' => __( 'Add New Topic' ),
    'new_item_name' => __( 'New Topic Name' ),
    'menu_name' => __( 'News Category' ),
  ); 
  register_taxonomy('news-category',array('news'), array(
    'hierarchical' => true,
    'labels' => $labels,
    'show_ui' => true,
    'show_admin_column' => true,
    'query_var' => true,
    'rewrite' => array( 'slug' => 'news-category' ),
  ));
 
}

 

Now if you want to create a non-hierarchical custom taxonomy like Tags, add this code in your active theme’s functions.php file.

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

 

That’s it. Know you will see something like this on your site’s admin section.

 

For more information on this, please visit this link.

Tags:

Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x