Why Use Custom Post Types in WordPress?

The use case scenarios for custom post types are not always obvious. WordPress already has the “category” functionality, and you can have child categories as well. So what’s the problem? Why would you want to create a completely new custom post type instead of just calling it a category?

It all comes down to the kind of URL you want. For example, say you have a couple of nested categories like this:

nested categories are a good use case for custom post types

I have a parent category called “WordPress”, and within that I have the subcategories of plugins and themes. Within themes, I have “Widgets”. All this is fine. You can configure the URL of each page so that the category name is displayed just as you want.

However, what happens when you view the archive for each category? Let’s say you want to look at “Widgets”. This is what you’ll see:

www.websitename/category/wordpress/themes/widgets/

Notice how the word “category” pops up in the URL of the archive. Why? Because you’re viewing the category archive that’s why! If you were viewing tags, you would see the word “tag” instead. That’s just the way taxonomies work.

So the solution is custom post types. Internally by default, WordPress has a custom post type called “Category” and so it shows up at the top of the URL structure even when you don’t want it to. Ideally in the above example, you want wordpress to be the top level category! and that’s exactly what custom post types allow you to do. So when you view all the posts for “WordPress”, you can have a URL that looks like this instead:

www.websitename/wordpress/themes/widgets/

In this example, WordPress is the new top level hierarchy sitting alongside “Category”. You can still make use of the categories – they can be incredibly useful, especially when it comes to easily querying for posts. But if you’re looking to make your URLs as clean and easy to use, then removing the “category” word from the archives is a great idea. It reduces the complexity by one, and has consequent SEO benefits as well!

Creating New Custom Post Types

I don’t want to go into the intricacies of creating a brand new custom post type here. However, for the above example, here is the code to create “WordPress” as a new top level custom taxonomy:

function create_new_taxonomy() {
    register_taxonomy(
        'wordpress',
        'post',
        array(
            'label' => __( 'WordPress' ),
            'rewrite' => array( 'slug' => 'wordpress' ),
            'hierarchical'  => true,
                                
        )
    );
}
add_action( 'init', 'create_new_taxonomy' );

You can customize this as you see fit. The “label” parameter in the array is the user friendly name for the taxonomy. So you can use a capital letter for it. The “slug” will be what is displayed in the URL. “Hierarchical” allows the it to accept children, so it behaves more like a regular category.

Using custom post types in this manner, you can create a more streamlined URL structure for your site. Both for SEO purposes, as well as for the benefit of customers.

Speak Your Mind

*