how to create custom post type in wordpress

how to create custom post type in wordpress

What is Custom Post Type in WordPress?

Custom Post type is also a post type like pages, posts, attachments.Wordpress do easily work with custom post type . you don’t want to make insert, update , delete or make any custom page for development. you can do all the things with custom post type and these features are like posts and you can create custom fields according to their use. By default, WordPress have these post types:

  • Post
  • Page
  • Revision
  • Nav Menu
  • Attachment

Custom post type by wordpress codex

 function create_customposttype() {
register_post_type( 'listings',
// CPT Options
'labels' => array(
'name' => __( 'listings' ),
'singular_name' => __( 'listing' )
'public' => true,
'has_archive' => true,
'rewrite' => array('slug' => 'listings'),
// Hooking up our function to theme setup
add_action( 'init', 'create_customposttype' );

In the above code , we are registering custom post type as “listings” in wordpress with codex functions (register_post_type()).register_post_type function have some parameters or arguments of array.These arguments are the options of “listings “custom post type.This array has two parts, the first part is labels, which itself is an array. The second part contains other arguments like public visibility, has archive, and slug that will be used in URLs for this post type.

Now We are creating post type with lots of options.

* 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', 'twentythirteen' ),
        'singular_name'       => _x( 'Movie', 'Post Type Singular Name', 'twentythirteen' ),
        'menu_name'           => __( 'Movies', 'twentythirteen' ),
        'parent_item_colon'   => __( 'Parent Movie', 'twentythirteen' ),
        'all_items'           => __( 'All Movies', 'twentythirteen' ),
        'view_item'           => __( 'View Movie', 'twentythirteen' ),
        'add_new_item'        => __( 'Add New Movie', 'twentythirteen' ),
        'add_new'             => __( 'Add New', 'twentythirteen' ),
        'edit_item'           => __( 'Edit Movie', 'twentythirteen' ),
        'update_item'         => __( 'Update Movie', 'twentythirteen' ),
        'search_items'        => __( 'Search Movie', 'twentythirteen' ),
        'not_found'           => __( 'Not Found', 'twentythirteen' ),
        'not_found_in_trash'  => __( 'Not found in Trash', 'twentythirteen' ),
// Set other options for Custom Post Type
    $args = array(
        'label'               => __( 'movies', 'twentythirteen' ),
        'description'         => __( 'Movie news and reviews', 'twentythirteen' ),
        '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'     => 'page',
    // 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 a lots of features in above custom post type like now it supports featured image , custom field,archive post it can be use in menu and etc.
So Its good and recommended method to use custom post type.

Displaying Custom Post Types on Your Site

You can easily show custom post type in any where in the page or templates its easy and and take less efforts.

Some steps related to use custom post type in website –

Displaying Custom Post Type Using Default Archive Template

Simply go to Appearance » Menus and add a custom link to your menu. If you are using SEO friendly permalinks then your CPT’s URL will most likely be something like this:
If you have selected id permalink then you can show it by


Using Custom Templates for CPT Archives and Single Entries

you can copy the file of your theme’s archive.php file into “archive-listings.php” template and you can customize this template according to their needs. Now whenever the archive page for your custom post type is accessed, this template will display the listings posts.

Displaying Custom Post Types on The Front Page

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;

WP Query For Custom Post type

you can fetch the records from a custom post type by given code.

$args = array( 'post_type' => 'listings', '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 posts matched your criteria.' ); ?></p>
<?php endif; ?>

In this code, firstly we have defined the post type and posts per page in the arguments for our new WP_Query class. After that we ran wordpress query to get the posts and displayed them inside the loop.

Leave a Reply

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