The Ultimate Guide to WordPress Custom Post Types

Reading Time: 11 minutes
697 Views

Inside this article we will see the concept i.e The Ultimate Guide to WordPress Custom Post Types. Article contains the classified information i.e The Complete Guide To WordPress Custom Post Types.

In WordPress, a post is a type of content that allows you to share your thoughts, ideas, and news with your audience. Posts are typically displayed in reverse chronological order on your website, with the most recent post appearing first.

Read More: How to Speed Up Your WordPress Website: A Beginner’s Guide

Let’s get started.

What is a WordPress Post Type?

In WordPress, a post type refers to a type of content that you can create and publish on your website. By default, WordPress includes these types of post types:

  • Post
  • Page
  • Attachment
  • Revision
  • Nav Menu

However, if you have specific needs for your website that are not met by the default post types, you can create custom post types to extend the functionality of your site. Custom post types allow you to create new types of content that are tailored to your specific needs, such as portfolios, testimonials, or products.

What is a Custom Post Type in WordPress?

Custom post type is a feature in WordPress that allows you to extend the functionality of your website beyond the standard “post” and “page” post types. It enables you to create custom types of content for your website, such as portfolios, testimonials, or products.

With custom post types, you can create unique content and categorize it in a way that makes sense for your specific website needs.

Custom post types are defined in your WordPress site using code, either by using a plugin or by adding the code directly to your theme. When you create a custom post type, you specify the name of the post type, the labels for it, and the capabilities for it.

You can also define custom taxonomies for your custom post type, which allow you to categorise and organize your content in a way that makes sense for your site.

Read More: Top Best Free WordPress Plugins for Image Optimization Tutorial

Once you have created your custom post type, you can display it on your website using custom templates, loops, or plugins.

Custom post types give you the flexibility to create content types that fit the unique needs of your website, making it easier to present your content in a way that is meaningful and relevant to your audience.

How To Create Custom Post Type in WordPress

Here, inside this article we will see two methods by which you can register a custom post type in wordpress.

  • Creating a Custom Post Type using WPCode Plugin (recommended)
  • Creating a Custom Post Type With a Plugin

Creating a Custom Post Type using WPCode Plugin (recommended)

Normally, only advanced users are recommended to add code to the theme’s functions.php file in order to create a custom post type. This is because even a small mistake can result in a broken site, and also updating the theme would erase the code.

However, we will use WPCode a wordpress plugin, the easiest and safest way for anyone to add custom code to your WordPress website.

Steps to Install and Use WPCode

Go to Plugins >> Add New, search for “WPCode

Once you search, click on activate to use it.

Once activated, navigate to Code Snippets » Add Snippet in your WordPress dashboard. Hove your mouse over “Add Your Custom Code (New Snippet)” and then click “Use Snippet

Next, you will be taken to the “Create Custom Snippet” screen.

Now, you can give your code snippet a title and toggle the switch to “Active

Once you do this, just paste the following code into the “Code Preview” area. This code will create a basic custom post type called “Movies” that will appear in your admin sidebar, and it will work with any theme.

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

It will register a CPT (Custom Post Type) Movies in your wordpress admin.

Read More: Powerful SEO Tips to Boost Traffic to Your Website

If you want more options in CPT as support for revisions, featured images, custom fields, etc. Need to use the above code in a way with more 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', 'twentytwentyone' ),
        'singular_name'       => _x( 'Movie', 'Post Type Singular Name', 'twentytwentyone' ),
        'menu_name'           => __( 'Movies', 'twentytwentyone' ),
        'parent_item_colon'   => __( 'Parent Movie', 'twentytwentyone' ),
        'all_items'           => __( 'All Movies', 'twentytwentyone' ),
        'view_item'           => __( 'View Movie', 'twentytwentyone' ),
        'add_new_item'        => __( 'Add New Movie', 'twentytwentyone' ),
        'add_new'             => __( 'Add New', 'twentytwentyone' ),
        'edit_item'           => __( 'Edit Movie', 'twentytwentyone' ),
        'update_item'         => __( 'Update Movie', 'twentytwentyone' ),
        'search_items'        => __( 'Search Movie', 'twentytwentyone' ),
        'not_found'           => __( 'Not Found', 'twentytwentyone' ),
        'not_found_in_trash'  => __( 'Not found in Trash', 'twentytwentyone' ),
    );
      
// Set other options for Custom Post Type
      
    $args = array(
        'label'               => __( 'movies', 'twentytwentyone' ),
        'description'         => __( 'Movie news and reviews', 'twentytwentyone' ),
        '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 );

In admin panel you will see,

Creating a Custom Post Type With a Plugin

Another easy method 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.

Read More: Laravel How To Update Record without Updating Timestamp

The first thing you need to do is install and activate the Custom Post Type UI plugin.

Go to CPT UI >> Add / Edit Post Types

To create your first CPT, Click on “Add New Post Type” tab.

First, you need to provide a slug for your custom post type, such as “movies” for Movies a CPT.

Now, if you want to add more options scroll down to that “Additional Labels” section.

Once you do, it will create Movies CPT.

We hope this article helped you to learn about The Ultimate Guide to WordPress Custom Post Types Tutorial in a very detailed way.

Online Web Tutor invites you to try Skillshike! Learn CakePHP, Laravel, CodeIgniter, Node Js, MySQL, Authentication, RESTful Web Services, etc into a depth level. Master the Coding Skills to Become an Expert in PHP Web Development. So, Search your favourite course and enroll now.

If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.

Sanjay KumarHello friends, I am Sanjay Kumar a Web Developer by profession. Additionally I'm also a Blogger, Youtuber by Passion. I founded Online Web Tutor and Skillshike platforms. By using these platforms I am sharing the valuable knowledge of Programming, Tips and Tricks, Programming Standards and more what I have with you all. Read more