1. Home
  2. Docs
  3. WordPress Developer
  4. Creating Custom Post Types In WordPress

Creating Custom Post Types In WordPress

By default WordPress comes with post and pages as the main content types. However you can create as many custom content types as you like, and these custom content types are referred to as Custom Post Types.

Custom post types are one of the more useful features in WordPress. Especially if you’re looking for a way to extend your favourite blogging platform into a fully-fledged content management system.

WordPress provides you with a handful of content types by default. They are: posts, pages, attachments, revisions, and navigation menus.
you can use pages to display the content that rarely changes and remains evergreen (such as About page or Contact page),
and then use posts to publish entries that might have an expiration date on them
If you want to publish other content that’s neither a page, nor a post then you can use Custom Post Types.
Custom post types can help you in many situations, based on the type of content you want to publish on your WordPress site.
Most commonly, a new custom post type is a good idea when you simply want to start publishing something that needs to be kept apart from posts and pages.

Like If you run a coupons site then you can probably use a custom post type called coupon.

Creating a Custom Post Type Manually

You can manually create your custom post type by adding the required code in your theme’s functions.php file or in a  plugin.

For the sake of a simple example we will be adding our new code to our functions.php file inside our theme folder. It is worth noting that this code will only be available on this particular theme installation. If you need this custom post type functionality within multiple themes then you will want to create a plugin.

 Register Post Type

Open your functions.php file and add the code below to it. Make sure the code is added before the closing PHP tag.

// Creates Coupon Reviews Custom Post Type
function coupon_seals_init() {
$args = array(
'label' => ' ',
'public' => true,
'show_ui' => true,
'capability_type' => 'post',
'hierarchical' => false,
'rewrite' => array('slug' => 'coupon-and-deals'),
'query_var' => true,
'menu_icon' => 'dashicons-tag',
'supports' => array(
register_post_type( 'coupon-and-deals', $args );
add_action( 'init', 'coupon_seals_init' );

  • coupon_seals_init() – Here we create a new function for our custom post type. We do this to limit conflicts with any other code inside our functions.php file. It is also a best practice when adding new code to any file like functions.php.
  • label – A plural descriptive name for the post type marked for translation. If you don’t declare a custom label, WordPress will use the name of the custom post type by default.
  • public – Whether a post type is intended to be used publicly either via the admin interface or by front-end users. WordPress sets this to false by default.Here we set it to true as we do what our custom post type to display publicly.
  • show_ui – Generates a default UI for managing this post type in the admin. You can set this to true or false. For the sake of usability, a UI in the admin area is always a good thing.
  • capability_type – Here we can declare what type of custom post type we will be dealing with. It is used to build the read, edit, and delete capabilities of a post or page. You can choose either post or page.
  • hierarchical – Whether the post type is hierarchical. Or in laymen’s terms, whether or not you can declare a parent page, child page, etc… of the post type. This is mainly intended for Pages. Here we declare it false so there’s no need to worry about it for our example.
  • rewrite – This rule is either true or false. The default is true so if slug argument is entered then the slug name is prepended to the posts. Our slug “movie-reviews” will be prepended to each new post of that type.
  • query_var – This rule is either true or false. It sets the post type name as a query variable.
  • menu_icon – This rule declares a custom icon for the admin area. Here we use a neat resource called dashicons that are included in WordPress already.
  • supports – This is usually an array of features the custom post type will support. Here we have quite a long list. These will tie into the admin area.
  • register_post_type($post_type, $args); – The register_post_type() is a function that WordPress recognizes as a custom post type generator. In this example it accepts two parameters which are the name of the post type itself and any arguments you would like to call.
  • add_action(‘init’, ‘movie_reviews_init’ ); – This line of code returns or calls our function so it fires and displays within our site.

Now that we have defined our custom post type inside our functions.php file, head to your WordPress dashboard to see that we did everything correctly. You should now have a new menu item on the left side of your screen titled Coupons.

WordPress  Common template partials structure

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.

Now we need to add the functionality that makes what we post actually appear on the front end of our website. Let’s create a new menu that will display a navigational link to our Coupons.

Appearance » Menus and add a custom link to your menu

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_coupon_post_types_to_query' );

functionadd_coupon_post_types_to_query( $query ) {
if ( is_home() && $query->is_main_query() )
$query->set( 'post_type', array( 'post', 'coupon-and-deals' ) );
return $query;

Don’t forget to replace coupon-and-deals 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.

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


$args = array( 'post_type' => 'coupon-and-deals', '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(); ?>
<?php wp_reset_postdata(); ?>
<?php else: ?>
<p><?php _e( 'Sorry, no coupon matched your criteria.' ); ?></p>
<?php endif; ?>

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.

 Create a Custom Post Type Template

Create a new file called page-coupon-and-deals.php inside your theme’s folder.

Inside the folder you’ll want to add code below to tell WordPress that it is indeed a new template file.

* Template Name: Coupon And Deals

Add following code to display Custom post type in template file.

$args = array( 'post_type' => 'coupon-and-deals', '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(); ?>
<?php wp_reset_postdata(); ?>
<?php else: ?>
<p><?php _e( 'Sorry, no coupon matched your criteria.' ); ?></p>
<?php endif; ?>

New template should appear in the dropdown under Page Attributes as Coupon And Deals.


Was this article helpful to you? Yes No

How can we help?