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 also have child categories. So what’s the problem? Why would you want to create an entirely 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
Nested Categories are an excellent Use Case for Custom Post Types

I have a parent category called “WordPress”; 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,” and 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,” 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 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 alongside “Category.” You can still make use of the categories – they can be handy, especially when it comes to efficiently querying for posts. But if you’re looking to make your URLs 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 explore the intricacies of creating a 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 custom_post_type() {
    $args = array(
        'labels' => array(
            'name' => __( 'My WordPress Posts' ),
            'singular_name' => __( 'My WordPress Post' ),
            'add_new' => __( 'Add New' ),
            'add_new_item' => __( 'Add New WordPress Post' ),
            'edit_item' => __( 'Edit WordPress Post' ),
            'new_item' => __( 'New WordPress Post' ),
            'view_item' => __( 'View WordPress Post' ),
        ),
        'public' => true,
        'hierarchical' => true,
        'has_archive' => true,
        'rewrite' => array('slug' => 'wordpress'),
        'show_in_rest' => true,
        'supports' => array('title', 'editor', 'thumbnail')
    );
    register_post_type('my-wordpress-posts', $args);
}
add_action('init', 'custom_post_type');

You can customize this as you see fit. The “labels” 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 it to accept children, so it behaves more like a regular category.

Once you’ve added the above code, here’s what it looks like on the WordPress dashboard:

New WordPress Custom Post Type
New WordPress Custom Post Type

Just create a new post for your custom post type as you would any other!

Using custom post types, you can create a more streamlined URL structure for your site – both for SEO purposes and for the benefit of customers.

About Bhagwad Park

I've been writing about web hosting and WordPress tutorials since 2008. I also create tutorials on Linux server administration, and have a ton of experience with web hosting products. Contact me via e-mail!

Speak Your Mind

*

WP-Tweaks