How to create custom post in WordPress without using plugin

What is Custom Post?

In WordPress CMS, Custom posts are similar to regular WordPress posts. The only difference between a regular post and a custom post is its post type in the database. The Post type of a regular post is only “post” but in the custom post, you can define your own post type like testimonials, books, stories, etc. You can create a custom post using Plugins or you don’t want to use the plugin and just want to keep it simple use the below method (function) to create a custom post.
You can also read: How to design a WordPress Website

Why do we need to create a custom post?

The requirement of custom posts on any website depends on the structure of the website. Custom post is required in such websites which require a specific type of pages in the website. Pages that have all the same content and mater structure and can be added in more quantity like, “Team”. For example, suppose there is a need to create a page for each team member in your company and the structure of the team page for all team members is going to be the same. So in this situation, we will create a custom post which we will name “Team”. And the post_type of this post will be “team”.

How to create custom post type WordPress without a plugin:

Below we have written a script to create custom posts. You can create single or multiple posts using this script,  You don’t need to do any customization in this script. Just copy the complete below code and past it in your theme functions.php file and change the post name with your own.

Follow the steps below to create custom posts in WordPress Website: 

Step 1:  Open the Funciton.php file from your active theme folder

Step 2:  In your function file,  add the name of your custom post in the below array “$custom_post“. You can add single or multiple custom posts

/*** Put the custom post name in the array  **/ 
$custom_posts = array('testimonial','team');  // create two custom post i.e testimonial and team 

Step 3: Copy the script below and paste it after the array you write in step 2.



/**** DO NOT EDIT BELOW SCRIPT ***/

foreach($custom_posts as $cp):

    $cfunc = function () use ($cp) {
	$cap_title = ucwords($cp);
	$lower_title = $cp;
        $labels = array(
        'name' => $cap_title,
        'singular_name' => $cap_title,
        'add_new' => 'Add New '.$cap_title,
        'add_new_item' => 'Add New '.$cap_title,
        'edit_item' => 'Edit '.$cap_title,
        'new_item' => 'New '.$cap_title,
        'all_items' => 'All '.$cap_title."s",
        'view_item' => 'View Testimonial',
        'search_items' => 'Search '.$cap_title.'s',
        'not_found' =>  'No Record Found',
        'not_found_in_trash' => 'No '.$lower_title.'s found in Trash', 
        'parent_item_colon' =>'',
        'menu_name' => $cap_title,
        );

        $args = array(
   
            'labels'              => $labels,
            'hierarchical'        => false,
            'public'              => true,
            'show_ui'             => true,
            'show_in_menu'        => true,
            'show_in_nav_menus'   => true,
            'show_in_admin_bar'   => true,
            'menu_position'       => 5,
            "rewrite" => array( "slug" => $cp , "with_front" => true ),
            'capability_type'     =>'post',
			'query_var' => true,
			'menu_icon' => 'dashicons-randomize',
			'supports' => array(
			'title',
			'editor',
			'excerpt',
			'trackbacks',
			'custom-fields',
			'comments',
			'revisions',
			'thumbnail',
			'author',
			'page-attributes',
			)
         );
        register_post_type( $cp, $args );  

    };
    add_action( 'init', $cfunc, 0 );

endforeach;

Step 3: save the file

Once you will save the file, go to the admin panel and check in the left menu, You will see 2 custom posts with the name of testimonial and team.  If you still have any issues, write your comment, I’ll reply to your comments.

You Might alos Like
Spread the love

Leave a Comment

12 − 10 =