custom post type

How can you create Custom Post Type in WordPress?

WordPress is often confused as a blogging platform but there is more to it than meets the eye. Over the years, WordPress evolved into a content management system. Through this you can create custom content type which is referred as Custom Post Types.

Let’s see how you too can create these custom post types in WordPress.

What is a Custom Post Type?

Over the years, WordPress evolved from a blogging platform into a more robust CMS but the term Custom Post type stuck with it. There are 5 types of post in WordPress:

  • Post
  • Page
  • Attachment
  • Revision
  • Navigation Menu

Creating custom post types is easy and you can call them whatever you want. For example, if you run a restaurant review website, you probably want to create a restaurant review post type. It will have custom fields and have custom category structure. Some other examples of custom post types are: Testimonials, Products, etc.

Creating a Custom Post Type Manually

The problem with plugin is that if you deactivate the plugin your custom post types will disappear as well. The data in these post will be there but the custom post type will be unregistered and you can’t access it from admin area.

You can manually create a custom post type by adding the required code in funtions.php.

// Our custom post type function
function create_custom_post_restaurant() {
register_post_type( 'restaurant',
// Custom Post Type Options
array(
'labels' => array(
'name' => __( 'Restaurant' ),
'singular_name' => __( 'Restaurant' )
),
'public' => true,
'has_archive' => true,
'rewrite' => array('slug' => 'restaurant'),
)
);
}
// Hooking up our function to theme setup
add_action( 'init', 'create_custom_post_restaurant' );
This code will deliver a post type "restaurant". Below is a detailed code with more options
/*
* Creating a function to create our Custom Post Type
*/
// replace 'theme-text-domain' with your theme text domain
function create_custom_post_restaurant() {
// Set UI labels for Custom Post Type
$labels = array(
'name'                => _x( 'Restaurant', 'Post Type General Name', 'theme-text-domain' ),
'singular_name'       => _x( 'Restaurant', 'Post Type Singular Name', 'theme-text-domain' ),
'menu_name'           => __( 'Restaurant', 'theme-text-domain' ),
'parent_item_colon'   => __( 'Parent Restaurant', 'theme-text-domain' ),
'all_items'           => __( 'All Restaurants', 'theme-text-domain' ),
'view_item'           => __( 'View Restaurant', 'theme-text-domain' ),
'add_new_item'        => __( 'Add New Restaurant', 'theme-text-domain' ),
'add_new'             => __( 'Add New', 'theme-text-domain' ),
'edit_item'           => __( 'Edit Restaurant', 'theme-text-domain' ),
'update_item'         => __( 'Update Restaurant', 'theme-text-domain' ),
'search_items'        => __( 'Search Restaurant', 'theme-text-domain' ),
'not_found'           => __( 'Not Found', 'theme-text-domain' ),
'not_found_in_trash'  => __( 'Not found in Trash', 'theme-text-domain' ),
);
// Set other options for Custom Post Type
$args = array(
'label'               => __( 'restaurant', 'theme-text-domain' ),
'description'         => __( 'Restaurant news and reviews', 'theme-text-domain' ),
'labels'              => $labels,
// Features this Custom Post Type supports in Post Editor
'supports'            => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'comments', 'revisions', 'custom-fields', ),
// You can associate this Custom Post Type with a taxonomy or custom taxonomy.
'taxonomies'          => array( 'categories' ),
/* A hierarchical Custom Post Type is like Pages and can have
* Parent and child items. A non-hierarchical Custom Post Type
* 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'     => 'page',
);
// Registering your Custom Post Type
register_post_type( 'restaurant', $args );
}
/* Hook into the 'init' action so that the function
* Containing our post type registration is not
* unnecessarily executed.
*/
add_action( 'init', 'create_custom_post_restaurant', 0 );

Here you can see more options to the custom post type. You can find your theme’s text domain inside style.css file in your theme directory.

Displaying Custom Post Types

Once the items are added into your new custom post types, you can display them on your website.
You can use these methods to display your custom post type:

Using Default Archive Template – Go to Appearance-> Menus and add a custom link.
Example:
https://example.com/restaurant
Now, this will be your link to all your custom posts (posts added in restaurant)

Using Custom Templates for Custom Post Types Archives & Single Entries – You can also customize template for custom post type archive. To do this, you need to create a new file in theme directory and name it archive-restaurant.php. Replace restaurant with the name of your custom post type. Copy the contents of archive.php file into archive-restaurant.php. You can also create a custom template for single entry display too by copying theme’s single.php into single-restaurant.php template and make changes in this file according to your requirements.

Displaying Custom Post Types on the front page – Custom post types will keep your custom content types away from regular posts. If you want to, you can display it with your regular post too by adding this code to functions.php:

add_action( 'pre_get_posts', 'get_posts_in_query' );
function get_posts_in_query( $query ) {
if ( is_home() && $query->is_main_query() )
$query->set( 'post_type', array( 'post', 'restaurant' ) );
return $query;
}

Queries for looping custom post type
You can run loop of queries in your templates , this way you can retrieve items from a custom post type.

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

Creating a Custom Post Type Using a Plugin

You can easily create a custom post type in WordPress with a plugin. If you are a beginner, plugin is the best way to add custom post type as it is completely safe and quite easy.

For this, the first step is to install and activate Custom Post Type UI plugin. Once it is activated, the plugin adds a new menu item in the admin menu. The new menu is named CPT UI.

Once this is done, go to CPT UI -> Add New to create a custom post type.

Through Custom Post Type UI plugin, you can also create custom taxonomies. The Add new custom post type page is divided into two columns. The left column will have the form that will create your custom post type and on the right, you can create a custom taxonomy (if you need).
In the left column where you have created custom post type, you’ll need to provide a name of your custom post type. The name shouldn’t exceed more than 20 characters.

The next step is to provide your custom post type a label. It will appear on your WordPress admin bar and this should be in plural (to make some sense, of course)

Once this is done, you’ll need to provide a singular form for the label. This form will be used by WordPress to show instructions and other elements. The last thing to do here is to enter a description for your custom post type. Once you have done this, click on the ”Create Custom Post Type” button and add your new custom post type.
Click on the Advanced Label Options and Advanced Options links to customize options for your custom post type.

There is much more you can do with custom post types. We hope this post helps you create custom post types to your liking in WordPress.
Enterprise Web Cloud is a leading web design company in Mississauga. We develop responsive web designs that are search engine friendly.