How to create a custom post type in WordPress with its own categories

Hi there,

All you need to do to add your own custom post type in WordPress with its own categories is add this code to your functions file. I’ve named mine “Our Work” because I used it for portfolio items which I wanted to keep seperate.

This adds a custom post type called Our Work. You can name this anything you want.

<?php
add_action( 'init', 'create_post_type' );
function create_post_type() {
  register_post_type( 'our-work', // Call this whatever you want. No spaces or weird characters
    array(
      'labels' => array(
        'name' => __( 'Our Work' ), // This is the label that will display in the sidebar menu
        'singular_name' => __( 'Our Work' ) // Make this singular from the name above. I just made it the same.
      ),
      'public' => true,
      'has_archive' => true,
      'taxonomies' => array( 'newcat' ), // Here you tell it which categories to use for this post type. Name this anything you want.
    )
  );
}
?>

This adds a taxonomy or categories to your custom post type.

<?php
add_action( 'init', 'create_newcat_taxonomy' );

function create_newcat_taxonomy() {
  register_taxonomy(
    'newcat', // This should be the same as the taxonomies name above so that it's linked
    'our-work', // This should be the same as the top register_post_type above so that it's linked to your custom post type
    array(
      'label' => 'Category', // This is just your category label that will display under your custom post type in the sidebar
      'hierarchical' => true,
    )
  );
}
?>

Please comment below if you have any questions.

This article was written by Francois View Profile
Hi. I'm currently 31 years old and I'm a full time Front-End Web Developer. I love cats and I love life.... sometimes. I created this blog when I was figuring out how Wordpress works and it eventually started to get a whole lot of traffic. I'm now trying to help fellow South Africans with general how to stuff for example how to renew your car license disc and also reviews like restaurants etc. I haven't done much yet but I will soon start to do more and more. I will try to write as many blog posts as I can. I hope you enjoy this blog and don't forget to leave a comment. Oh and you're welcome to write a guest post or review. Just click on Write Guest Post on the top main menu.
Twitter | Facebook

Leave a Reply

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

nine + 19 =