skip to Main Content

WordPress Create Custom Post Type

WordPress create custom post type


WordPress provides awesome functionality to register post type. Using register post type functionality, We can create the new post type like Post. Page with custom fields and taxonomy. Today we learn how we can create custom post type on WordPress.

We used register_post_type for creating the custom post type. register post type should invoke through “init” action. It will not work properly If we called before init action. you can use ‘register_post_type’ action in themes and plugins.

Registering custom post type is awesome functionality for creating user-friendly plugins functionality like Slider listing, Book listing, Portfolio listing etc.. Also, it provides add and edit interface, We don’t need to take rush for add, Edit, Filter functionality.

Let’s see the fastest and easy way for registering our custom post type

function thecodingstuff_custom_posttype() {
    $args = array(
      'public' => true,
      'label'  => 'Sliders'
    );
    register_post_type( 'thecodingstuff_slider', $args );
}
add_action( 'init', 'thecodingstuff_custom_posttype' );

Above we learn the basic syntax for registering the custom WordPress Post type. There are we have passed single label “Sliders” for thecodingstuff_slider post type. Also, WordPress provides functionality to customize all Labels for the post type.

Let’s see how we can create the register post type with customize the labels.

function thecodingstuff_slider_posttype() {
    $labels = array(
        'name'               => _x( 'Sliders', 'post type general name', 'thecodingstuff' ),
        'singular_name'      => _x( 'Slider', 'post type singular name', 'thecodingstuff' ),
        'menu_name'          => _x( 'Sliders', 'admin menu', 'thecodingstuff' ),
        'name_admin_bar'     => _x( 'Slider', 'add new on admin bar', 'thecodingstuff' ),
        'add_new'            => _x( 'Add New', 'Slider', 'thecodingstuff' ),
        'add_new_item'       => __( 'Add New Slider', 'thecodingstuff' ),
        'new_item'           => __( 'New Slider', 'thecodingstuff' ),
        'edit_item'          => __( 'Edit Slider', 'thecodingstuff' ),
        'view_item'          => __( 'View Slider', 'thecodingstuff' ),
        'all_items'          => __( 'All Sliders', 'thecodingstuff' ),
        'search_items'       => __( 'Search Sliders', 'thecodingstuff' ),
        'parent_item_colon'  => __( 'Parent Sliders:', 'thecodingstuff' ),
        'not_found'          => __( 'No Sliders found.', 'thecodingstuff' ),
        'not_found_in_trash' => __( 'No Sliders found in Trash.', 'thecodingstuff' )
    );

    $args = array(
        'labels'             => $labels,
        'description'        => __( 'Thecodingstuff Sliders', 'thecodingstuff' ),
        'public'             => true,
        'publicly_queryable' => true,
        'show_ui'            => true,
        'show_in_menu'       => true,
        'query_var'          => true,
        'rewrite'            => array( 'slug' => 'thecodingstuff_slider' ),
        'capability_type'    => 'post',
        'has_archive'        => true,
        'hierarchical'       => false,
        'menu_position'      => null,
        'supports'           => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments' )
    );

    register_post_type( 'thecodingstuff_slider', $args );
}

add_action( 'init', 'thecodingstuff_slider_posttype' );

So it’s too easy to create the custom post type on WordPress.

If you face any issue with creating the new post type, write the comment below.

Saroj

I am a web designer and developer part-time blogger. Write something on some free time. Find my every day thought here :)

Leave a Reply

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

Back To Top