How to Create WordPress Custom Post Type and Taxonomy

In this WordPress tutorial, we will cover the following queries:

  • How to create a custom post type with taxonomy (category) in WordPress?
  • How to create a custom post type in WordPress plugin?
  • How to create a custom post type archive page in WordPress?

There are two ways to create a custom post type in WordPress:

1. By plugin

If you are looking for a quick way to create a custom post type and taxonomy then try the Custom Post Type UI plugin. It provides an easy to use interface for registering and managing custom post types and taxonomies for your website.

You can create or edit custom post type and taxonomy with the help of this plugin very easily without any technical knowledge requiring.

2. By Code

Copy the following WordPress custom post type code and paste into your custom plugin or functions.php

Create custom post type code:

// Creating a Jobs Custom Post Type

function tia_jobs_custom_post_type() {

$labels = array(

'name'                => __( 'Jobs' ),
'singular_name'       => __( 'Job'),
'menu_name'           => __( 'Jobs'),
'parent_item_colon'   => __( 'Parent Job'),
'all_items'           => __( 'All Jobs'),
'view_item'           => __( 'View Job'),
'add_new_item'        => __( 'Add New Job'),
'add_new'             => __( 'Add New'),
'edit_item'           => __( 'Edit Job'),
'update_item'         => __( 'Update Job'),
'search_items'        => __( 'Search Job'),
'not_found'           => __( 'Not Found'),
'not_found_in_trash'  => __( 'Not found in Trash')

);

$args = array(

'label'               => __( 'jobs'),
'description'         => __( 'Best TIA Jobs'),
'labels'              => $labels,
'supports'            => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'revisions', 'custom-fields'),
'public'              => true,
'hierarchical'        => false,
'show_ui'             => true,
'show_in_menu'        => true,
'show_in_nav_menus'   => true,
'show_in_admin_bar'   => true,
'has_archive'         => true,
'can_export'          => true,
'exclude_from_search' => false,
'yarpp_support'       => true,
'taxonomies'       => array('post_tag'),
'publicly_queryable'  => true,
'capability_type'     => 'page'

);

register_post_type( 'jobs', $args );

}

add_action( 'init', 'tia_jobs_custom_post_type', 0 );

The above code will create a new section called Jobs into your WP admin backend. Make sure to change custom post type arguments from code according to your requirement, In this WordPress tutorial I have created custom post type for ‘jobs’ label.

Create custom taxonomy for a custom post type code:

Now, what if you want to create Custom Taxonomy (category) named Company. With register_taxonomy function, you could simply create a taxonomy.

// Let us create Taxonomy for Custom Post Type
add_action( 'init', 'tia_create_jobs_custom_taxonomy', 0 );

//create a custom taxonomy name it "type" for your posts
function tia_create_jobs_custom_taxonomy() {

  $labels = array(

    'name' => _x( 'Companies', 'taxonomy general name' ),
    'singular_name' => _x( 'Company', 'taxonomy singular name' ),
    'search_items' =>  __( 'Search Companies' ),
    'all_items' => __( 'All Companies' ),
    'parent_item' => __( 'Parent Company' ),
    'parent_item_colon' => __( 'Parent Company:' ),
    'edit_item' => __( 'Edit Company' ),
    'update_item' => __( 'Update Company' ),
    'add_new_item' => __( 'Add New Company' ),
    'new_item_name' => __( 'New Company Name' ),
    'menu_name' => __( 'Companies' ),

  );

  register_taxonomy('types',array('jobs'), array(

    'hierarchical' => true,
    'labels' => $labels,
    'show_ui' => true,
    'show_admin_column' => true,
    'query_var' => true,
    'rewrite' => array( 'slug' => 'Company' ),

  ));

}

The above code will generate taxonomy named “Company” into Jobs custom post type.

Create a custom taxonomy template for your theme:

When you add a custom taxonomy to a WordPress theme, you can display the content into a custom template using one of WordPress’ taxonomy theme templates.

  • taxonomy-{taxonomy}-{slug}.php Use this to create a theme template for a particular location, such as taxonomy-event-thirsty-thursday.php for the term “Thirsty Thursday”.
  • taxonomy-{taxonomy}.php If the taxonomy name were event then WordPress will look for taxonomy-event.php.
  • taxonomy.php This is the default template for all custom taxonomies.
  • archive.php If no taxonomy specific template is found, then the taxonomy that lists pages will use the archive template.
  • index.php It will be used If no other template is found.

Let’s use taxonomy-event.php it to display our content into the event taxonomy archive file.

Code for custom taxonomy archive page template:

<?php
/**
* Events taxonomy archive
*/
get_header();
$term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );
?>
<div class="wrapper">
<div class="primary-content">
<h1 class="archive-title"><?php echo apply_filters( 'the_title', $term->name ); ?> News</h1>

<?php if ( !empty( $term->description ) ): ?>
<div class="archive-description">
<?php echo esc_html($term->description); ?>
</div>
<?php endif; ?>

<?php if ( have_posts() ): while ( have_posts() ): the_post(); ?>

<div id="post-<?php the_ID(); ?>" <?php post_class('post clearfix'); ?>>
<h2 class="post-title"><a href="<?php the_permalink(); ?>" rel="bookmark"><?php the_title(); ?></a></h2>
<div class="content clearfix">
<div class="post-info">
<p><?php the_time(get_option('date_format')); ?> by <?php the_author_posts_link(); ?></p>
</div><!-- end .post-info -->
<div class="entry">
<?php the_content(); ?>
</div>
</div>
</div><!-- end #post-XX -->

<?php endwhile; ?>

<div class="navigation clearfix">
<div class="alignleft"><?php next_posts_link('« Previous Entries') ?></div>
<div class="alignright"><?php previous_posts_link('Next Entries »') ?></div>
</div>

<?php else: ?>

<h2 class="post-title">No News in <?php echo apply_filters( 'the_title', $term->name ); ?></h2>
<div class="content clearfix">
<div class="entry">
<p>It seems there isn't anything happening in <strong><?php echo apply_filters( 'the_title', $term->name ); ?></strong> right now. Check back later, something is bound to happen soon.</p>
</div>
</div>

<?php endif; ?>
</div><!-- end .primary-content -->

<div class="secondary-content">
<?php get_sidebar(); ?>
</div><!-- end .secondary-content -->

<?php get_footer(); ?>

if you are looking for WP_Query for custom post type and tax_query then please click here to get complete details.

Comments are closed, but trackbacks and pingbacks are open.