Request Quote

Create Custom Post Type WordPress

Custom Post Type is same as WordPress default post having different post type from WordPress. As we know WordPress regular post has post type ‘post’. So, to create our own post type WordPress provide a function named register_post_type(). This function allows us to define new post type by supported features, labels.

Note that you must call register_post_type() before the admin_menu and after the after_setup_theme action hooks. A good hook to use is the init hook (codex info).

Let’s start with example –

<?php

function create_custom_post() {

$args() = array();

register_post_type( 'type-name', $args );

}

add_action( 'init', 'create_custom_post' );

?>

In above code, type-name in register_post_type() is the name of custom post type. Here any name can be set according to need of post type.

To register post type according to our needs, we will go through some options and add them to $args array. Let’s add some options (labels, features).

<?php

function create_custom_post() {

$args = array(
           'labels' => array(
                          'name'         => __('Name for type in admin menu'),
                          'singular_name => __('Singular Name'),
                          'add_new'      => __('Add New'),
                       ),
           'description' => 'Description about post type',
           'public'      => true,
           'menu_position' => 10,
           'supports'    => array('title', 'editor', 'thumbnail', excerpt')
         );
register_post_type( 'type-name' 'create_custom_post' );

}

?>

Labels

This option defines the different labels for custom post type. Foe example we set for name, singular name, add new. There can be more also like edit, view, search, etc.

Description

The description as name suggests contain some explanation about custom post type like what it does.

Menu Position

In this we can define the position of post type in admin menu. The higher we set it , lower the menu will be placed.

Supports

This option defines WordPress default controls as regular post to custom post type. For a full list take a look at the arguments section in the Codex.

Reference https://codex.wordpress.org/Function_Reference/register_post_type.

Support

Still have any issue feel free to add a ticket and let us know your views to make the code better https://webkul.uvdesk.com/en/customer/create-ticket/

Thanks for Your Time! Have a Good Day!

 

. . .

Comments (2)

Add Your Comment

  • Ayush Singh
    N1 (Y)
  • Thakur Amit Chauhan
    Nice work ..!!
  • css.php

    Index