Add Tag and Category support to WordPress Custom Post Types

Custom post types in WordPress are becoming popular lately. Lots of people are interested in them. Especially theme developers, they are using custom post types to market their themes. But the problem is some of these themes lack of certain things like tags and categories.

I came through this theme on themeforest, which has a custom post type ‘Reviews’. It was a great feature rich theme and had lots of sales under its name. Author has assigned a custom category (Taxonomy) type to the reviews as well. But there’s no way to add tags to custom posts. So ‘Reviews’ post type is completely separated from regular posts, categories and tags.

The theme has built in related post function based on tags. But this feature is not available to reviews since tags are not available to reviews. It’s fair if someone need this on their reviews. It’s author’s fault to miss it.

So I see some people asked author how to fix it. Authors reply was “I’ll include it in next update” but months have passed since that comment and issue didn’t seems to be fixed yet. I don’t expect anything more from author. He doesn’t like anyone touching his code. Adding a tag or category support to custom post types is very easy. It only takes few seconds to achieve it. It’s just not right for someone to wait months for an update to fix it. Here’s how you can do it.

Custom post types and all its supported functions are located at functions.php in your theme’s root folder. We need to edit it, so connect to your hosting with ftp and open functions.php. Now we need to locate the code that says WordPress to register a custom post type. It’ll look like below,

add_action('init', ' review_register_post_type');
function review_register_post_type() {
register_post_type('Reviews', array(
'labels' => array(
'name' => ' Reviews',
'singular_name' => 'Review',
'add_new' => 'Add new Review',
'edit_item' => 'Edit Review',
'new_item' => 'New Review',
'view_item' => 'View Review',
'search_items' => 'Search Reviews',
'not_found' => 'No Reviews found',
'not_found_in_trash' => 'No Reviews found in Trash'
),
'public' => true,
'supports' => array(
'title',
'excerpt'
),
));
}

Your code may look slightly different because of the name of your custom post. Now we need to add simple code to it. First note the following part at the end of above code,

'excerpt'
),
));
}

Now add below code in a new line after second line (line that has comma mark and close bracket mark).

'taxonomies' => array('category', 'post_tag')

Final code will look like below,
[php highlight=”3″]’excerpt’
),
‘taxonomies’ => array(‘category’, ‘post_tag’)
));
}

That code will enable category box and tag box in your custom post editor. If you only need to add tags, make it look like ‘taxonomies’ => array( ‘post_tag’) and for category only ‘taxonomies’ => array( ‘ category’)

That’s it, save and close file. Problem is now fixed.

Tags
Show More

Tharindu

Hey!! I'm Tharindu. I'm from Sri Lanka. I'm a part time freelancer and this is my blog where I write about everything I think might be useful to readers. If you read a tutorial here and want to hire me, contact me here.

4 Comments

    1. Hello DR,
      Make sure you place code for categories and tags on right place, See example on last code snippet, I have highlighted code for categories and tags. Your code should be exactly same like that.

    1. Hello DR,
      Make sure you place code for categories and tags on right place, See example on last code snippet, I have highlighted code for categories and tags. Your code should be exactly same like that.

Leave a Reply

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

Back to top button

We use cookies to give you the best online experience. By agreeing you accept the use of cookies in accordance with our cookie policy.

Close