How to Create a Custom Post Type in WordPress

WordPress is all about posts and pages. But sometimes, you need more than the posts and pages while creating your own website. In this tutorial, we will look at Custom Post Types, one of WordPress’s most powerful features, and how the introduction of this wonderful feature boosted WordPress to new heights.

WordPress post types make it simple to arrange your content by constructing various new buckets to put your own types of content into so you can have a beautiful site.

In this tutorial we get to know about creating a post type by following some simple and easy steps.

SO! Let’s dig into this.

What is a Post Type?

A post type functions in the same way that “Pages” and “Posts” do in the WordPress admin area. When you create a post type, it will appear in the admin menu and have the same editor and title fields as “Posts.” Once it’s set up, you can add content to it in the same way you’d add a blog “Post” to the admin’s “Posts” section.

You can create different types of post types like, movies, reviews, news, Hotels, etc.

Why do we create custom Post Types?

We can separate our default posts with newly created custom posts.

So basically It distinguishes our regular posts from others. It’s as simple as that!

Creating a Custom Post type plugin..

We shall create a plugin which will display a News post type, when enabled.

Follow the below steps to create the plugin:

  • Step1: Create Plugin directory

Open your WordPress Plugin directory and create a new folder named  News Plugin

  • Step2: Create News.php file

Now create a News.php file and save it  under the News Plugin folder.

  • Step3: Add Header
<?php

/*
Plugin Name: News plugin
Plugin URI: https://eplugins.org/
Description: Declares a plugin that will create a custom post type displaying Latest News.
Version: 1.0
Author: jen Niles
Author URI: https://jenniles.eplugins.org/
License: GPLv2
*/

?>



  • Step4: Register Function to add custom Post type

Before the closing of the PHP command, type the following line of code to execute the custom function named create_News .

add_action( 'init', 'create_news' );
  • Step5: Function Implementation

Now we shall provide the implementation of the function Create_news

function create_news() {
  $labels = array(
    'name'               => _x( 'News', 'post type general name' ),
    'singular_name'      => _x( 'News', 'post type singular name' ),
    'add_new'            => _x( 'Add New', 'News' ),
    'add_new_item'       => __( 'Add New News' ),
    'edit_item'          => __( 'Edit News' ),
    'new_item'           => __( 'New News' ),
    'all_items'          => __( 'All News' ),
    'view_item'          => __( 'View News' ),
    'search_items'       => __( 'Search News' ),
    'not_found'          => __( 'No News found' ),
    'not_found_in_trash' => __( 'No News found in the Trash' ), 
    'menu_name'          => 'News'
  );
  $supports = array(
'title', // post title
'editor', // post content
'author', // post author
'thumbnail', // featured images
'excerpt', // post excerpt
'custom-fields', // custom fields
'comments', // post comments
'revisions', // post revisions
'post-formats', // post formats
);
  $args = array(
 
    'supports' => $supports,
'labels' => $labels,
'description' => 'Holds our News and specific data',
'public' => true,
'taxonomies' => array( 'category', 'post_tag' ),
'show_ui' => true,
'show_in_menu' => true,
'show_in_nav_menus' => true,
'show_in_admin_bar' => true,
'can_export' => true,
'capability_type' => 'post',
 'show_in_rest' => true,
'query_var' => true,
'rewrite' => array('slug' => 'news'),
'has_archive' => true,
'hierarchical' => false,
'menu_position' => 6,
'menu_icon' => 'dashicons-megaphone',
  );
  register_post_type( 'news', $args ); 
}
  • Step6: set icon for the custom post type

With the WP Dashicons, adding menu icons to post types has become very simple.

Set the menu_icon argument to one of the Dashicon names. For our post type, we can use the megaphone  icon by using the value dashicons-megaphone .

  • Step7: Activate the Plugin

Activate the plugin and that’s it, you have a new custom post type which has a text editor, publishing and featured image controls, comment control and the custom fields editor.

  • Step8: Add a new News item and Publish It

Wrapping Words…

In my next tutorial I shall cover some more features of the Custom Post Types like creating an archived page, creating a template to display single news, custom taxonomies,etc. Please feel free to provide your valuable suggestions.

Leave a Comment

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