Creating Custom Post Types in WordPress Themes

Posted on 18th June 2023

WordPress themes are very flexible. Developers can extend the functionality of a WordPress theme by creating custom post types. Custom post types allow you to add new content types to your WordPress site. In this article, we will show you how to create custom post types in WordPress themes.

What is a Custom Post Type?

A custom post type is a post type that is not part of the default WordPress post types. Default WordPress post types are: post, page, attachment, and revision.

Custom post types are created by developers to extend the functionality of WordPress. They are usually created to add new content types to WordPress. For example, a developer may create a custom post type for products, events, or books.

Creating Custom Post Types in WordPress Themes

Creating a custom post type is a two-step process. First, you need to register the custom post type using the register_post_type() function. Second, you need to add support for custom post types in your theme.

Step 1: Registering the Custom Post Type

The first step is to register the custom post type using the register_post_type() function. This function takes two arguments:

  • $post_type: The name of the custom post type.
  • $args: An array of arguments. These arguments are used to configure the custom post type.

The $post_type argument is the name of the custom post type. It should be a unique name. The $args argument is an array of arguments. These arguments are used to configure the custom post type.

The register_post_type() function can be used in the init action hook.

Here is an example of how to register a custom post type:

function wpdocs_register_my_post_types() {
    register_post_type( 'product', array(
        'labels' => array(
            'name' => __( 'Products' ),
            'singular_name' => __( 'Product' )
        ),
        'public' => true,
        'has_archive' => true,
    ) );
}
add_action( 'init', 'wpdocs_register_my_post_types' );

In the example Above, we registered a custom post type called product. We set the labels, public, and has_archive arguments.

The labels argument is used to set the labels for the custom post type. The labels array contains the following keys:

  • name: The plural name of the custom post type.
  • singular_name: The singular name of the custom post type.

The public argument is used to set whether the custom post type is publicly visible. The default value is false.

The has_archive argument is used to set whether the custom post type has an archive page. The default value is false.

For more information on the register_post_type() function, please see the Codex.

Step 2: Adding Support for Custom Post Types in Your Theme

The second step is to add support for custom post types in your theme. This can be done by adding the following code to your theme’s functions.php file:

add_action( 'after_setup_theme', 'my_theme_supports' );
function my_theme_supports() {
    add_theme_support( 'post-types', array( 'product' ) );
}

In the example above, we added support for the product custom post type.

For more information on adding support for custom post types in themes, please see the Codex.

Conclusion

In this article, we showed you how to create custom post types in WordPress themes. Custom post types are a great way to extend the functionality of your WordPress site.

In the previous article, we looked at how to create custom post types in WordPress themes. In this article, we will look at how to add taxonomies to custom post types.

Taxonomies are a way of grouping content together. For example, you might have a custom post type for products and you might want to group those products by type, size, or color. That’s where taxonomies come in.

There are two types of taxonomies: hierarchical and non-hierarchical. Hierarchical taxonomies are similar to categories in WordPress. Non-hierarchical taxonomies are similar to tags in WordPress.

To add a taxonomy to a custom post type, you need to use the register_taxonomy() function. This function takes a number of arguments, but the most important ones are the labels and the post type(s) to which the taxonomy should be attached.

The labels argument is an array of labels for the taxonomy. The most important labels are the name and singular_name labels. These are used to display the taxonomy in the WordPress admin interface.

The post_type argument is an array of post types to which the taxonomy should be attached. In most cases, you will want to attach the taxonomy to the custom post type that you created in the previous article.

Here is an example of how to register a hierarchical taxonomy for a custom post type:

function my_theme_register_taxonomies() {
$labels = array(
‘name’ => ‘Product Types’,
‘singular_name’ => ‘Product Type’,
);
$args = array(
‘labels’ => $labels,
‘hierarchical’ => true,
‘show_ui’ => true,
‘show_admin_column’ => true,
‘query_var’ => true,
‘rewrite’ => array( ‘slug’ => ‘product-type’ ),
);
register_taxonomy( ‘product-type’, ‘product’, $args );
}
add_action( ‘init’, ‘my_theme_register_taxonomies’ );

In the example above, we’ve registered a hierarchical taxonomy called “Product Type” for the custom post type “Product”.

If you want to register a non-hierarchical taxonomy, you need to set the ‘hierarchical’ argument to false.

function my_theme_register_taxonomies() {
$labels = array(
‘name’ => ‘Product Sizes’,
‘singular_name’ => ‘Product Size’,
);
$args = array(
‘labels’ => $labels,
‘hierarchical’ => false,
‘show_ui’ => true,
‘show_admin_column’ => true,
‘query_var’ => true,
‘rewrite’ => array( ‘slug’ => ‘product-size’ ),
);
register_taxonomy( ‘product-size’, ‘product’, $args );
}
add_action( ‘init’, ‘my_theme_register_taxonomies’ );

In the example above, we’ve registered a non-hierarchical taxonomy called “Product Size” for the custom post type “Product”.

Once you have registered a taxonomy, it will appear in the WordPress admin interface when you edit a post of the relevant type. For example, if you have a custom post type of “Products” and you have registered a taxonomy of “Product Type”, then when you edit a product, you will see a “Product Type” metabox where you can select the relevant product type.