Post types in WordPress are a valuable tool for organizing and managing various types of content on your website. The two primary built-in post types in WordPress are posts and pages.
While posts are designed to be updated regularly and will make up the bulk of the content on your site, pages are designed to stick around longer and provide information on the kinds of things that don’t change so often like “contact details” or “about us” page on a business website.
What is a Custom Post Type in WordPress?
If you’re looking to expand your site’s functionality beyond the standard blog format, custom post types can be a game-changer. With WordPress’s robust and highly customizable content management system, the possibilities are virtually endless.
For example, if you’re interested in creating an e-commerce website, you’ve probably heard of WooCommerce, one of the most popular WordPress plugins. This plugin is a perfect example of how custom post types can be implemented to create a specialized type of content tailored to your specific needs.
By utilizing custom post types, you can create a seamless and intuitive user experience that’s specifically designed to meet your unique requirements. So, whether you’re running an online store, a portfolio website, or anything in between, custom post types can help you take your site to the next level.
Creating a Custom Post Type Without Plugin
In this tutorial, we’ll explore a method for creating a custom post type without relying on any plugins. If you prefer a plugin-free approach, this guide will provide valuable insights into crafting a custom post type that aligns with your requirements. By following these steps, you’ll be able to display your custom post type on an archive page. We’ll even include a screenshot of the end result to give you a visual representation. So, let’s dive in!
This tutorial uses the function.php file from the active themes, to avoid losing your changes when you update or change your theme, you may want to use a child theme or create a plugin for creating your custom post type.
Creating the Custom Post Type
The first step for creating a custom post type without a plugin is preparing the function to register your post type.
function wpp_register_post_type() { } add_action( 'init', 'wpp_register_post_type' );
The above function will contain all the custom codes required to create our custom post type. Starting from defining labels and arguments and then registering the custom post type using the register_post_type()
function.
Defining the Labels
For this tutorial, we are going to create a custom post type called “shoes” for adding shoe product pieces of information. So, we will add the code below inside the curly bracket from the previous code:
//the label format is 'label_type' => __('label_display', 'textdomain') $labels = array( 'name' => __( 'Shoes', 'plural', 'wppagebuilders' ), 'singular_name' => __( 'Shoe', 'singular', 'wppagebuilders' ), 'add_new' => __( 'New Shoe', 'wppagebuilders' ), 'add_new_item' => __( 'Add New Shoe', 'wppagebuilders' ), 'edit_item' => __( 'Edit Shoe', 'wppagebuilders' ), 'new_item' => __( 'New Shoe', 'wppagebuilders' ), 'view_item' => __( 'View Shoes', 'wppagebuilders' ), 'search_items' => __( 'Search Shoes', 'wppagebuilders' ), 'not_found' => __( 'No Shoes Found', 'wppagebuilders' ), 'not_found_in_trash' => __( 'No Shoes found in Trash'), );
That’s it for the label we use for this tutorial, but if you want to add more, there are still so many label types you can use for your custom post type.
Defining the Arguments
Once you’ve added the label, we need to define it in the arguments along with other arguments as follows:
$args = array( 'labels' => $labels, 'has_archive' => true, 'public' => true, 'hierarchical' => false, 'supports' => array( 'title', 'editor', 'excerpt', 'custom-fields', 'thumbnail', 'page-attributes' ), 'taxonomies' => 'category', 'rewrite' => array( 'slug' => 'shoe' ), 'show_in_rest' => true );
Here is what the arguments from the code above do:
- labels: The array of labels we defined before
- has_archive: true, enable archive for the custom post type
- public: true, enable the post type to be included in the WordPress dashboard, the search result and custom queries
- hierarchical: false, stop the custom post type from behaving like pages
- supports: an array to enable features for the custom post type i.e.: custom fields
- taxonomies: define ‘category’ as taxonomies for the custom post type
- rewrite: replace the post type slug with ‘shoe’ i.e.: www.wppagebuilders.com/shoe/sneakers
- show_in_rest: true, enable Gutenberg editor for the custom post type
Register the Post Type
Finally, to complete the function to create the custom post type, we need to add the register_post_type()
function to register it. Add the following code snippet below the arguments code.
register_post_type( 'wpp_shoe', $args );
In the snippet above we tell WordPress to register the ‘wpp_shoe’ post type with the argument we defined before.
Here is the complete code:
function wpp_register_post_type() { //the label format is 'label_type' => __('label_display', 'textdomain') $labels = array( 'name' => __( 'Shoes', 'plural', 'wppagebuilders' ), 'singular_name' => __( 'Shoe', 'singular', 'wppagebuilders' ), 'add_new' => __( 'New Shoe', 'wppagebuilders' ), 'add_new_item' => __( 'Add New Shoe', 'wppagebuilders' ), 'edit_item' => __( 'Edit Shoe', 'wppagebuilders' ), 'new_item' => __( 'New Shoe', 'wppagebuilders' ), 'view_item' => __( 'View Shoes', 'wppagebuilders' ), 'search_items' => __( 'Search Shoes', 'wppagebuilders' ), 'not_found' => __( 'No Shoes Found', 'wppagebuilders' ), 'not_found_in_trash' => __( 'No Shoes found in Trash'), ); $args = array( 'labels' => $labels, 'has_archive' => true, 'public' => true, 'hierarchical' => false, 'supports' => array( 'title', 'editor', 'excerpt', 'custom-fields', 'thumbnail', 'page-attributes' ), 'taxonomies' => array( 'category' ), 'rewrite' => array( 'slug' => 'shoe' ), 'show_in_rest' => true ); register_post_type( 'wpp_shoe', $args ); } add_action( 'init', 'wpp_register_post_type' );
Once you’ve added the code, click the Update File button to save the changes then reload your WordPress admin dashboard. Now you can start adding content to your new custom post type and see the result from the front end.
If you can’t access the posts from your custom post type, it’s because WordPress hasn’t yet written the permalinks for them. To fix this, navigate to Settings → Permalinks and just click the Save Changes button to tell WordPress to create the URLs for them.
Displaying the Custom Post Type on Archive Page
When you add custom post types to your website, they are typically designed to function similarly to regular posts. And since you enabled the 'has_archive'
argument, this means they can be displayed on archive pages making it easier for visitors to browse and access your content. Archive pages act as a collection or index of posts, allowing users to explore different categories, tags, or custom taxonomies. Here are several ways to display your custom post type in an archive.
- Displaying all the posts from a custom post type on an archive page
- Displaying custom post type from a taxonomy on an archive(i.e.: display archive from a ‘sports shoes’ category)
- Mix the main blog page that displays the post from the ‘post’ post type with your custom post type.
Displaying All Posts from Custom Post Type
To display the custom type on an archive, you can utilize the theme Customizer and add a menu to navigate to the archive. To access the Customizer from your WordPress dashboard, go to Appearance then click the Customize menu.
Once you’re in the customizer, open up the Menu block and create a new menu or edit an existing one if you already have one.
Next is adding the items to the menu. For our menu, we add the home page, the about page, and our custom post type archive page the ‘Shoes’ page. Publish the menu once it’s ready.
Now, you can access your custom post type archive from the menu on your website. Here is what our archive page looks like.
Displaying Custom Post Type Archive from a Single Taxonomy
To display an archive of your custom post type from a single taxonomy, you can utilize the theme customizer like the previous method which is by adding items from the taxonomy (i.e.: categories, tags, or custom taxonomy) of your choice to your website menu. Here is the screenshot of adding an item from Categories block to your website menu from the Customizer.
By default, the category archive page only can show the ‘post’ type and nothing else. So, before you can show an archive page from your custom post type using the category taxonomy, you need to hook it first into the query. To do so, you can add the following code at the bottom of your theme’s functions.php file.
function custom_post_type_category_archive( $query ) { if( (is_category() || is_tag()) && $query->is_archive() && empty( $query->query_vars['suppress_filters'] ) ) { $query->set( 'post_type', array( 'post', 'wpp_shoe' )); } } add_action( 'pre_get_posts', 'custom_post_type_category_archive' );
Replace the ‘wpp_shoe’ with your own registered custom post type ( ‘wpp_shoe’ is this tutorial’s registered custom post type).
By now, you can display your custom post type by category taxonomy of your choice. Here is the screenshot of our custom post type archive with ‘Sport Shoes’ category.
Adding Custom Post Type to the Main Blog Page
By default, the main blog page typically displays only the regular ‘post’ type content. However, if you wish to incorporate your custom post type into this page alongside the blog posts, you can achieve that by adding the following code snippet to your theme’s functions.php file.
function custom_post_on_blog_page() { global $wp_query; $query = $wp_query; if ( $query->is_home() && $query->is_main_query() ) { $query->set ('post_type', array( 'post', 'wpp_shoe')); } } add_action( 'pre_get_posts', 'custom_post_on_blog_page' );
Replace the ‘wpp_shoe’ with your own registered custom post type.
Now, your main blog post will have the custom post type mixed in. Here is the screenshot of the result on our end.
The Bottom Line
By utilizing custom post types, you can expand your site’s functionality beyond the standard blog format. You can create a specialized type of content tailored to your specific needs with it.
This tutorial shows you how to a plugin-free approach to create a custom post type and display it on an archive page.