WordPress Development comes with many features like simple user interface, Plain post editor, theme customization options and much more. In this post, I will be focusing on one of the cores and best feature i.e. Custom post types and taxonomies. Although WordPress comes with few default post types and taxonomies, it lets you extend the feature by adding custom post types and taxonomies. This article is about what are default post types and taxonomies and how we can create our own.
What are Post Types?
Post Types is a term that refers to different types of content on a WordPress site, for understanding you can say its content type. WordPress comes with five default post types:
1. Post – (Post Type: ‘post’)
Posts in WordPress are a post type used for creating news feeds, blog post, news and updating your website. All posts are stored in the wp_posts table of the database.
2. Page – (Post Type: ‘page’)
Page is another type of post, but they are used to create pages with fixed information and static pages mostly. Pages can use different templates, organized in a hierarchical structure. Pages are normally cannot be assigned to categories and tags.
3. Revision– (Post Type: ‘revision’)
Revision keeps a copy of every version of the post and draft copy as well. Revisions keep track of changes of the published post. Published posts are declared parent of revision post in parent_post column.
4. Attachment – (Post Type: ‘attachment’)
As the name suggests, it holds the attachments (media files). It holds name, description of the file uploaded.
5. Navigation Menu Item -(Post Type: ‘nav_menu_item’)
All the Menu items will be stored in this post type in the database.
what are Taxonomies?
Taxonomies is a categorizing technique used to categorize posts. You can say Taxonomies is a way of grouping things together. For example, you have a news based website where you post health, sports, current affairs, international news and more. You can categorize each of your post to the closest taxonomies for keeping things together. This way you can show them all in one single page as the main page as well.
Following are the default WordPress Taxonomies:
1. Category
‘category’ taxonomy helps you group post into a relevant category. You can see specific categories of a WordPress site at [site-name]/category/name’ types of URLs.
2. Tag
‘post_tag’ taxonomy is similar to the category. But they are less devoted and more flexible. You can create them during a post as well. You can see specific categories of a WordPress site at [site-name]/tag/name’ types of URLs.
3. Link Category
‘link_category’ is used internally for organizing and to categorize the links. We seldom see Link Categories on the website, but they are used to define groups for displaying on sidebars.
4. Post Formats
‘post_formats’ was introduced in WordPress 3.1 and is used by a theme to customize its presentation of a post.
Before we learn how we can create custom post types and taxonomies, let’s find out the when we need them.
When we need custom post types and taxonomies?
Although you can add any content to post, categorize them with categories and tags, however, there are conditions when you need to have custom post types and taxonomies. Here are some of them:
- You may want to display some specific content differently from posts and pages.
- When it cannot be part of your pages, you need to have a completely new page.
- The content you are posting may not look like a normal post of your blog. So you need new post type.
- When even categories and tags cannot help you group some content, you need custom taxonomies.
So let’s start how we can create custom post types and taxonomies.
Creating Custom Post Type
To create a custom post type you can use register_post_type() function. You can add new post type by its label, availability and other features.
You must also call this function before admin_menu and after the after_setup_theme action hooks.
Here is an example where we add a new post type “Product.”
register_post_type() function receives two important augments, Label and public. Label defines the name of post type while public makes it appear on admin screen and site itself. There are more arguments that this function accepts, you can check the usage here.
Adding Featured image support to your post type
To add featured image support to your custom post type, you need to add it the theme settings. To do that open the functions.php and find this line:
add_theme_support( ‘post-thumbnails’, array( ‘post’, ‘page’ ) );
Replace it with
add_theme_support( ‘post-thumbnails’, array( ‘post’, ‘page’, products’ ) );
Save the file, and now you can add featured image to your custom post as well.
Creating Custom Taxonomy
When tags and categories can’t fulfill your needs, you can create custom taxonomies of your own. You can use register_taxonomy() function to create the custom taxonomy. Let’s register a taxonomy named ‘People.’
Above code snippet creates a taxonomy “People.” It is also defined to work for posts. Also, we have changed the slug URL to “/person/.” Capabilities argument will default capabilities to the same user as posts. Above code would allow any user with the custom capability of “edit_guides”, to assign taxonomy to post and user with custom “publish_guides” capability.
You can complete arguments details and usage here.
How to use Taxonomies?
Once created you can see this taxonomy it appears as a new Meta box on posts for you. To add terms to an object using your taxonomy, you must use wp_set_object_terms() function.
Here’s an example of adding the term “Tim” to post ID number 12 in the “person” taxonomy:
wp_set_object_terms( 12, ‘Tim’, ‘person’ );
Once you save the file, you will be able to assign categories to it.