How to Create Custom Post Types in WordPress

Do you want to learn how to easily create custom post types in WordPress? Custom post types transform a WordPress site from aΒ blogging platformΒ into a powerful Content Management System (CMS).

Basically, they allow you to go beyond posts and pages by creating different content types for your website.

In this article, we’ll show you how to easily create custom post types in WordPress. We’ll teach you two methods and you can choose one that looks easier to you.

What is Custom Post Type in WordPress?

Custom post types are content types likeΒ posts and pages. Since WordPress evolved from a simple blogging platform into a robust CMS, the term post stuck to it. However, a post type can be any kind of content.

By default, WordPress comes with these post types:

  • Post
  • Page
  • Attachment
  • Revision
  • Nav Menu

You can create your own custom post types and call them whatever you want.

For instance, if you run a movie review website, then you would probably want to create a movie reviews post type. This post type can have different custom fields and even its own custom category structure.

Other examples of post types are Portfolio, Testimonials, Products, etc.

ManyΒ popular WordPress pluginsΒ already use custom post types to store data on your WordPress website. The following are a few top plugins that use custom post types.

  • WooCommerce – Adds a product custom post type to your WordPress site.
  • WPForms – Creates a wpforms post type to store all your forms
  • MemberPress – Adds a memberpressproduct custom post type

When do I need a custom post type?

Check out our article aboutΒ when do you really need custom post types or taxonomies in WordPress.

Also take a look at pluginthemehub Deals and Glossary sections. These are custom post types that we created to keep these sections separate from our daily blog articles. It helps us better organize our website content.

You will also notice that we are usingΒ custom taxonomiesΒ for them instead of categories or tags.

That being said, let’s take a look at how to easily create custom post types in WordPress for your own use.

Method 1. Creating a Custom Post Type – The Easy Way

The easiest way to create a custom post type in WordPress is by using a plugin. This method is recommended for beginners because it is safe and super easy.

The first thing you need to do is install and activate theΒ Custom Post Type UIΒ plugin. Upon activation, the plugin will add a new menu item in your WordPress admin menu calledΒ CPT UI.

Now go toΒ CPT UI Β» Add NewΒ to create a new custom post type.

First, you need to provide a slug for your custom post type. ThisΒ slugΒ will be used in the URL and in WordPress queries, so it can only contain letters and numbers.

Below that, you need to provide the plural and singular names for your custom post type.

Next, you can optionally click on the link that says β€˜Populate additional labels based on chosen labels’. Doing so will fill in the rest of the label fields down below.

Scroll down to the β€˜Additional Labels’ section and from here you can provide a description for your post type and change labels.

First, you need to provide a slug for your custom post type. ThisΒ slugΒ will be used in the URL and in WordPress queries, so it can only contain letters and numbers.

Below that, you need to provide the plural and singular names for your custom post type.

Next, you can optionally click on the link that says β€˜Populate additional labels based on chosen labels’. Doing so will fill in the rest of the label fields down below.

Scroll down to the β€˜Additional Labels’ section and from here you can provide a description for your post type and change labels.

For instance, you can choose not to make a post type hierarchical like pages or reverse chronological like posts.

Below the general settings, you will see the option to select which editing features this post type would support. Simply check the options that you want to be included.

Finally, click on the β€˜Add Post Type’ button to save and create your custom post type.

That’s all, you have successfully created your custom post type. You can go ahead and start adding content.

We will show you how to display your custom post type on your website later in this article.

Creating a Custom Post Type Manually

The problem with using a plugin is that your custom post types will disappear when the plugin is deactivated. Any data you have in those custom post types will still be there, but your custom post type will be unregistered and will not be accessible from theΒ admin area.

If you are working on a client site and do not want to install another plugin, then you can manually create your custom post type by adding the required code in your theme’sΒ functions.phpΒ file or in aΒ site-specific pluginΒ (See:Β Custom Post Types Debate functions.php or Plugin).

First, we will show you a quick and fully working example so that you understand how it works. Take a look at this code:

// Our custom post type function
function create_posttype() {
Β  Β  register_post_type( ‘movies’,
Β  Β  // CPT Options
Β  Β  Β  Β  array(
Β  Β  Β  Β  Β  Β  ‘labels’ => array(
Β  Β  Β  Β  Β  Β  Β  Β  ‘name’ => __( ‘Movies’ ),
Β  Β  Β  Β  Β  Β  Β  Β  ‘singular_name’ => __( ‘Movie’ )
Β  Β  Β  Β  Β  Β  ),
Β  Β  Β  Β  Β  Β  ‘public’ => true,
Β  Β  Β  Β  Β  Β  ‘has_archive’ => true,
Β  Β  Β  Β  Β  Β  ‘rewrite’ => array(‘slug’ => ‘movies’),
Β  Β  Β  Β  Β  Β  ‘show_in_rest’ => true,
Β  Β  Β  Β  )
Β  Β  );
}
// Hooking up our function to theme setup
add_action( ‘init’, ‘create_posttype’ );

What this code does is that it registers a post type ‘movies’Β with an array of arguments. These arguments are the options of our custom post type.

This array has two parts, the first part is labeled, which itself is an array. The second part contains other arguments like public visibility, has archive, slug, and show_in_rest enables block editor support.

Now let’s take a look at a detailed piece of code that adds more options to your custom post type.

/*
* Creating a function to create our CPT
*/
function custom_post_type() {
// Set UI labels for Custom Post Type
Β  Β  $labels = array(
Β  Β  Β  Β  ‘name’Β  Β  Β  Β  Β  Β  Β  Β  => _x( ‘Movies’, ‘Post Type General Name’, ‘twentytwenty’ ),
Β  Β  Β  Β  ‘singular_name’Β  Β  Β  Β => _x( ‘Movie’, ‘Post Type Singular Name’, ‘twentytwenty’ ),
Β  Β  Β  Β  ‘menu_name’Β  Β  Β  Β  Β  Β => __( ‘Movies’, ‘twentytwenty’ ),
Β  Β  Β  Β  ‘parent_item_colon’Β  Β => __( ‘Parent Movie’, ‘twentytwenty’ ),
Β  Β  Β  Β  ‘all_items’Β  Β  Β  Β  Β  Β => __( ‘All Movies’, ‘twentytwenty’ ),
Β  Β  Β  Β  ‘view_item’Β  Β  Β  Β  Β  Β => __( ‘View Movie’, ‘twentytwenty’ ),
Β  Β  Β  Β  ‘add_new_item’Β  Β  Β  Β  => __( ‘Add New Movie’, ‘twentytwenty’ ),
Β  Β  Β  Β  ‘add_new’Β  Β  Β  Β  Β  Β  Β => __( ‘Add New’, ‘twentytwenty’ ),
Β  Β  Β  Β  ‘edit_item’Β  Β  Β  Β  Β  Β => __( ‘Edit Movie’, ‘twentytwenty’ ),
Β  Β  Β  Β  ‘update_item’Β  Β  Β  Β  Β => __( ‘Update Movie’, ‘twentytwenty’ ),
Β  Β  Β  Β  ‘search_items’Β  Β  Β  Β  => __( ‘Search Movie’, ‘twentytwenty’ ),
Β  Β  Β  Β  ‘not_found’Β  Β  Β  Β  Β  Β => __( ‘Not Found’, ‘twentytwenty’ ),
Β  Β  Β  Β  ‘not_found_in_trash’Β  => __( ‘Not found in Trash’, ‘twentytwenty’ ),
Β  Β  );
Β  Β  Β 
// Set other options for Custom Post Type
Β  Β  Β 
Β  Β  $args = array(
Β  Β  Β  Β  ‘label’Β  Β  Β  Β  Β  Β  Β  Β => __( ‘movies’, ‘twentytwenty’ ),
Β  Β  Β  Β  ‘description’Β  Β  Β  Β  Β => __( ‘Movie news and reviews’, ‘twentytwenty’ ),
Β  Β  Β  Β  ‘labels’Β  Β  Β  Β  Β  Β  Β  => $labels,
Β  Β  Β  Β  // Features this CPT supports in Post Editor
Β  Β  Β  Β  ‘supports’Β  Β  Β  Β  Β  Β  => array( ‘title’, ‘editor’, ‘excerpt’, ‘author’, ‘thumbnail’, ‘comments’, ‘revisions’, ‘custom-fields’, ),
Β  Β  Β  Β  // You can associate this CPT with a taxonomy or custom taxonomy.
Β  Β  Β  Β  ‘taxonomies’Β  Β  Β  Β  Β  => array( ‘genres’ ),
Β  Β  Β  Β  /* A hierarchical CPT is like Pages and can have
Β  Β  Β  Β  * Parent and child items. A non-hierarchical CPT
Β  Β  Β  Β  * is like Posts.
Β  Β  Β  Β  */Β 
Β  Β  Β  Β  ‘hierarchical’Β  Β  Β  Β  => false,
Β  Β  Β  Β  ‘public’Β  Β  Β  Β  Β  Β  Β  => true,
Β  Β  Β  Β  ‘show_ui’Β  Β  Β  Β  Β  Β  Β => true,
Β  Β  Β  Β  ‘show_in_menu’Β  Β  Β  Β  => true,
Β  Β  Β  Β  ‘show_in_nav_menus’Β  Β => true,
Β  Β  Β  Β  ‘show_in_admin_bar’Β  Β => true,
Β  Β  Β  Β  ‘menu_position’Β  Β  Β  Β => 5,
Β  Β  Β  Β  ‘can_export’Β  Β  Β  Β  Β  => true,
Β  Β  Β  Β  ‘has_archive’Β  Β  Β  Β  Β => true,
Β  Β  Β  Β  ‘exclude_from_search’ => false,
Β  Β  Β  Β  ‘publicly_queryable’Β  => true,
Β  Β  Β  Β  ‘capability_type’Β  Β  Β => ‘post’,
Β  Β  Β  Β  ‘show_in_rest’ => true,
Β  Β  );
Β  Β  Β 
Β  Β  // Registering your Custom Post Type
Β  Β  register_post_type( ‘movies’, $args );
}
/* Hook into the ‘init’ action so that the function
* Containing our post type registration is not
* unnecessarily executed.
*/
add_action( ‘init’, ‘custom_post_type’, 0 );

As you can see, we have added many more options to the custom post type with this code. It will add more features like support for revisions, featured image, custom fields, and more.

We have also associated this custom post type with a custom taxonomy called genres.

You may also notice the part where we have set the hierarchical value to be false. If you would like your custom post type to behave like Pages, then you can set this value to true.

Another thing to be noticed is the repeated usage of twentytwenty string, this is called text-domain. If your theme isΒ translation ready, and you want your custom post types to be translated, then you will need to mention text domain used by your theme.

You can find your theme’s text domain inside style.cssΒ file in your theme directory. The text domain will be mentioned in the header of the file.

Displaying Custom Post Types on Your Site

WordPress comes with built-in support for displaying your custom post types. Once you have added a few items into your new custom post type, it is time to display them on your website.

There are a couple of methods that you can use, each one has its own benefits.

Displaying Custom Post Type Using Default Archive Template

First, you can simply go toΒ Appearance Β» MenusΒ and add a custom link to your menu. This custom link is the link to your custom post type.

If you are using SEO friendly permalinks then your CPT’s URL will most likely be something like this:
Β 
http://example.com/movies
Β 
If you are not using SEO friendly permalinks, then your custom post type URL will be something like this:
Β 
http://example.com/?post_type=movies
Β 
Don’t forget to replace example.com with your own domain name and movies with your custom post type name.
Β 
Save your menu and then visit the front-end of your website. You will see the new menu you added, and when you click on it, it will display your custom post type archive page using the archive.php template file in your theme.
Β 
Using Custom Templates for CPT Archives and Single Entries
Β 
If you don’t like the appearance of the archive page for your custom post type, then you can use dedicated template for custom post type archive.
Β 
To do that all you need to do is create a new file in your theme directory and name it archive-movies.php. Replace movies with the name of your custom post type.
Β 
For getting started, you can copy the contents of your theme’s archive.php file into archive-movies.php template and then start modifying it to meet your needs.
Β 
Now whenever the archive page for your custom post type is accessed, this template will be used to display it.
Β 
Similarly, you can also create a custom template for your post type’s single entry display. To do that you need to create single-movies.php in your theme directory. Don’t forget to replace movies with the name of your custom post type.
Β 
You can get started by copying the contents of your theme’s single.php template into single-movies.php template and then start modifying it to meet your needs.

Displaying Custom Post Types on The Front Page

One advantage of using custom post types is that it keeps your custom content types away from your regular posts. However, if you would like them to display among your regular post, then you can do so by adding this code into your theme’s functions.php file or a site-specific plugin:

add_action( ‘pre_get_posts’, ‘add_my_post_types_to_query’ );
function add_my_post_types_to_query( $query ) {
Β  Β  if ( is_home() && $query->is_main_query() )
Β  Β  Β  Β  $query->set( ‘post_type’, array( ‘post’, ‘movies’ ) );
Β  Β  return $query;
}

Don’t forget to replaceΒ moviesΒ with your custom post type.

Querying Custom Post Types

If you are familiar with the coding and would like to run loop queries in your templates, then here is how to do that (Related:Β What is a Loop?).

By querying the database, you can retrieve items from a custom post type.

<?php
$args = array( ‘post_type’ => ‘movies’, ‘posts_per_page’ => 10 );
$the_query = new WP_Query( $args );
?>
<?php if ( $the_query->have_posts() ) : ?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<h2><?php the_title(); ?></h2>
<div class=”entry-content”>
<?php the_content(); ?>
</div>
<?php endwhile;
wp_reset_postdata(); ?>
<?php else:Β  ?>
<p><?php _e( ‘Sorry, no posts matched your criteria.’ ); ?></p>
<?php endif; ?>

In this code, first, we have defined the post type and posts per page in the arguments for our new WP_Query class.

After that, we ran our query, retrieved the posts and displayed them inside the loop.

Displaying Custom Post Types in Widgets

You will notice that there is a defaultΒ widgetΒ in WordPress to display recent posts, but it does not allow you to choose a custom post type.

What if you wanted to display the latest entries from your newly created post type in a widget? There is an easy way to do this.

First thing you need to do is install and activate theΒ Ultimate Posts WidgetΒ plugin. Upon activation, simply go toΒ Appearance Β» WidgetsΒ and drag and drop the Ultimate Posts widget to a sidebar.

This powerful widget will allow you to show recent posts from any post types. You can also display post excerpts with a read more link or even show a featured image next to post title.

Configure the widget by selecting the options you want and by selecting your custom post type. After that save your changes and see the widget in action on your website.

More Advance Custom Post Type Tweaks

There is so much more you can do with your custom post types. You can learn to add yourΒ custom post types in main RSS feedΒ or create aΒ separate feed for each custom post type.

For more hacks, see our list of theΒ most useful WordPress custom post types tutorials.

If you’re looking for a no-code solution to customize your custom post type archive pages, then we recommend taking a look at aΒ WordPress page builder pluginΒ likeΒ Beaver BuilderΒ orΒ DiviΒ because they both can help you do that.

We hope this article helped you learn how to create custom post types in WordPress. You may also want to see our guide on how toΒ increase your website trafficΒ with practical tips.

If you liked this article, then please subscribe to ourΒ YouTube ChannelΒ for WordPress video tutorials. You can also find us onΒ TwitterΒ andΒ Facebook.

Leave a Reply

Your email address will not be published. Required fields are marked *

error: Content is protected !!

Pin It on Pinterest

Add address