How to add Custom Post Type and Custom Taxonomies in WordPress

WordPress has brilliant features, which make it highly flexible in terms of customization. Among them are Custom Post Types and Custom Taxonomies.

Today we will be learning how to create and use them in our WordPress project.

Creating Custom Post Type

Let’s directly jump into the coding and do some cool stuff. Fire up your favourite code editor and create a new file under current theme with the name ‘project-post-type.php’.

Now, copy and paste following code in the created file and save it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
<?php 

// Add new post type for Project upon WordPress initialization
add_action('init', 'project_init');

function project_init() 
{
    //Labels for cusotm post type.
    $project_labels = array(
        'name' => _x('Projects', 'post type general name'),
        'singular_name' => _x('Project', 'post type singular name'), 
        'all_items' => __('All Projects'),
        'add_new' => _x('Add new project', 'project'),
        'add_new_item' => __('Add new project'),
        'edit_item' => __('Edit project'),
        'new_item' => __('New project'),
        'view_item' => __('View project'),
        'search_items' => __('Search in project'),
        'not_found' =>  __('No project found'),
        'not_found_in_trash' => __('No project found in trash'), 
        'parent_item_colon' => ''
    );
    
//Arguments
    $args = array(
        'labels' => $project_labels,
        'public' => true,
        'publicly_queryable' => true,
        'show_ui' => true, 
        'query_var' => true,
        'rewrite' => true,
        'capability_type' => 'post',
        'hierarchical' => false,
        'menu_position' => 5,
        'supports' => array('title','editor','author','thumbnail','excerpt','comments','custom-fields'),
                'taxonomies' => array( 'project-cat' ),
        'has_archive' => 'project'
    ); 
    register_post_type('project',$args);
}
?>

Now, just open your browser and browse to wp-admin to see the new post type added to the left side navigation. The WordPress function register_post_type() registers a new post type with the arguments of an array containing labels and other properties to edit the behavior of the post type.

To learn more about the arguments used, following the this link.

Custom Post Type Added

Custom Post Type

This type of post type can be used in any kind of purpose. In our example we are creating project post type to be used in a creative portfolio. Which will be differentiating with the normal posts and be more handy for client to edit the content without any confusion.

Furthermore, you can add post meta or custom fields to add each of the post type for numerous uses. For example a post type ‘Project’ may contain custom field like link to website, author, client name etc.

Creating a custom field is out of the scope of this topic but you can give a quick search in Google or install ‘Advanced custom fields’ WordPress Plugin to make use of them.

Creating Custom Taxonomies

In WordPress, custom taxonomies gives a class to post type. There are mainly two type of taxonomies.

  • Hierarchical Taxonomy
  • Non-hierarchical Taxonomy

For example, ‘Posts’ is a post type and ‘category’ is hierarchical and ‘tags’ are non-hierarchical taxonomy. Now, you might have got the glimpse of what taxonomies are, in WordPress.

An hierarchical taxonomy is a taxonomy which has parents and children. Whereas, non-hierarchical taxonomy has no hierarchy to mention and are independent of each other.

To create the a custom taxonomy, copy the following code just under the code mentioned above.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?php 
//Add new taxonomy upon WordPress initialization
add_action( 'init', 'project_create_taxonomies', 0 );

function project_create_taxonomies() 
{
    // Project Categories
    register_taxonomy('project-cat',array('project'),array(
        'hierarchical' => true,
        'label' => 'Project Categories',
        'singular_name' => 'Project Category',
        'show_ui' => true,
        'query_var' => true,
        'rewrite' => array('slug' => 'project-cat' )
    ));
}
?>

The above code applies register_taxonomy() WordPress function to register a new taxonomy and attach it to the ‘project’ Custom Post Type.

Now, Browse to wp-admin to see the effect.

Custom Taxonomy

The taxonomy we added is hierarchical one, to add a non-hierarchical taxonomy just change the ‘hierarchical’ attribute to ‘false’.

To add a newly created taxonomy column to the Post Type listing, add the following code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
//'project' is the registered post type name
add_filter( 'manage_project_posts_columns', 'prj_cpt_columns' );
add_action('manage_project_posts_custom_column', 'prj_cpt_custom_column', 10, 2);

function prj_cpt_columns($defaults) {
    $defaults['project-cat'] = 'Project Category';//project-cat is registered taxonomy name.
    return $defaults;
}

function prj_cpt_custom_column($column_name, $post_id) {
    $taxonomy = $column_name;
    $post_type = get_post_type($post_id);
    $terms = get_the_terms($post_id, $taxonomy);
 
    if ( !empty($terms) ) {
        foreach ( $terms as $term )
            $post_terms[] = "<a href='edit.php?post_type={$post_type}&{$taxonomy}={$term->slug}'> " . esc_html(sanitize_term_field('name', $term->name, $term->term_id, $taxonomy, 'edit')) . "</a>";
        echo join( ', ', $post_terms );
    }
    else echo '<i>Not assigned.</i>';
}

The above column will add a new column with taxonomy links for each of the post in custom post type listing page on the admin side.

Here is my part of Custom Post Type and Taxonomy, now you can add your own and follow the WordPress Codex to know more. Keep experimenting, WordPress is a great platform to start upon.

Sponsored Links

facebooktwittergoogle_plusredditpinterestlinkedinmail

SIGN UP TO OUR NEWSLETTER

Stay tuned with latest updates on Web technologies, subscribe to our newsletter. We'll not spam your inbox.

Speak Your Mind

dialog-information.png

Code highlighting instructions

Use {code type=language} for code highlighting. For example, if you are using Javascript code then use the following to wrap.

{code type=javascript}
//Your code here
{/code}

or for PHP, use:

{code type=PHP}
// Your code here
{/code}

 

*

CommentLuv badge

Security Code