It’s been a really long time since WordPress has been around. It single-handedly powers 60.4% of the internet in 2020. It’s so successful that its competition, Wix, stole the code. So what makes WordPress so special? I believe it’s all about how easily you could create custom content. WordPress uses Custom Post Type(CPT) & Taxonomies to create those content.

Definition

Post Type: In the simplest terms, different types of content is what you call a post type. For example, blog posts, recipes, pages, portfolios are all different types of content and hence should be different post types. From WordPress:

Post Type: In the simplest terms, different types of content is what you call a post type. For example, blog posts, recipes, pages, portfolios are all different types of content and hence should be different post types. 1From WordPress WordPress houses lots of different types of content and they are divided into something called Post Types. A single item is called a post however this is also the name of a standard post type called posts

Taxonomy: Taxonomy in WordPress is a way of grouping the post type with respect to a relationship. The most default of them would be categories or tags. 2From WordPress.org A taxonomy within WordPress is a way of grouping posts together based on a select number of relationships. By default, a standard post will have two taxonomy types called Categories and Tags which are a handy way of ensuring related content on your website is easy for visitors to find

Let’s Get Our Hands Dirty With CPT

I’m sure if you are here, you wouldn’t need to be told that, you need to create a plugin to create a post type. WordPress prefers all post types to be built as plugins, this is because the data won’t be lost even if the theme is changed. For example purpose, we will be creating the most cliche example in the whole of internet history – ‘Recipies’.

function create_posttype_recipie() {
  $recpie_labels = array(
    'name'               => _x( 'Recipies'),
    'singular_name'      => _x( 'Recipie',),
    'add_new'            => _x( 'Add New'),
    'add_new_item'       => __( 'Add New Recipie'),
    'edit_item'          => __( 'Edit Recipie' ),
    'new_item'           => __( 'New Recipie' ),
    'all_items'          => __( 'All Recipies' ),
    'view_item'          => __( 'View Recipie' ),
    'search_items'       => __( 'Search Recipies' ),
    'not_found'          => __( 'No recipie found' ),
    'not_found_in_trash' => __( 'No recipies found in trash' ), 
    'parent_item_colon'  => ’,
    'menu_name'          => 'Recipies'
  );
  $arguments = array(
    'labels'        => $recpie_labels,
    'description'   => 'A custom post type to create recipies and display them in the wordpress front end.',
    'public'        => true,
    'menu_position' => 10,
    'supports'      => array( 'title', 'editor', 'thumbnail', 'excerpt', 'comments' ),
    'has_archive'   => true,
  );
  register_post_type( 'recipie', $arguments ); 
}
add_action( 'init', 'create_posttype_recipie' );

So, let’s go through the code line by line, some of it is self-explanatory. Custom post types are loaded using the ‘init’ action, ‘init’ is used to initialize processes by plugins after WordPress is loaded and before the headers are sent.

The function used here is create_posttype_recipie, which will use register_post_type to register the post type. The arguments given in are labels, description of the post type, whether the post type is public or not, the menu position of the post type in the WordPress dashboard menu, the supported fields (here – title, editor, thumbnail or featured image, excerpt, and comments.), whether it is archivable. The labels part is self-explanatory. That’s it. It’s that simple to create custom post types in WordPress.

Now Taxonomies

Creating custom taxonomies is very similar to creating CPT. We use register_taxonomy just like for post types. Let’s create cuisine, shall we?

function recipie_cusine_taxonomy() {

$labels = array(
'name' => __( 'Cuisines'),
'singular_name' => __( 'Cuisine '),
'search_items' => __( 'Search Cuisines'),
'all_items' => __( 'All Cuisines'),
'edit_item' => __( 'Edit Cuisine'),
'update_item' => __( 'Update Cuisine'),
'add_new_item' => __( 'Add New Cuisine'),
'new_item_name' => __( 'New Cuisine Name'),
'menu_name' => __( 'Cuisines'),
);

$arguments = array(
'labels' => $labels,
'hierarchical' => false,
'sort' => true,
'args' => array( 'orderby' => 'term_order' ),
'rewrite' => array( 'slug' => 'cusines' ),
'show_admin_column' => true
'rewrite' => array( 'slug' => 'cusine' )
);

register_taxonomy( 'cusine', array( 'recipie' ), $arguments);
}

add_action( 'init', 'recipie_cusine_taxonomy' );

When we dive into the function, the arguments that need to be given in are the name of the taxonomy, the post types in which the taxonomy is applicable which needs to be passed in as array and then similar to the arguments in post type – the labels, if it’s hierarchical, if sorting is required for the particular taxonomy, and if it should display a column for the taxonomy on its post type listing. You can also add a rewrite that will be used to rewrite the permalink of the taxonomy.

That’s all there is to it. Pretty sure after you have seen how simple it’s to do this, you won’t ever go back to the Advanced Custom Fields Plugin.

Comments to: Ultimate Guide To Custom Post Types & Taxonomies

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