Naveed Anjum
+92 313 514 6669

Create Custom Post Type in WordPress

Following is the code to create Custom Post Type for Services and its Category in WordPress.

/**
 * This file registers the Services custom post type and its category
 *
 * @package     Naveed
 * @link        http://naveedanjum.info
 * Author:      Naveed
 * License URI: http://www.gnu.org/licenses/gpl-2.0.txt
 */

// Register the Services custom post type
function naveed_register_services() {
    $slug = apply_filters( 'naveed_services_rewrite_slug', 'services' );        
    $labels = array(
        'name'                  => _x( 'Services', 'Post Type General Name', 'naveed' ),
        'singular_name'         => _x( 'Service', 'Post Type Singular Name', 'naveed' ),
        'menu_name'             => __( 'Services', 'naveed' ),
        'name_admin_bar'        => __( 'Services', 'naveed' ),
        'archives'              => __( 'Item Archives', 'naveed' ),
        'parent_item_colon'     => __( 'Parent Item:', 'naveed' ),
        'all_items'             => __( 'All Services', 'naveed' ),
        'add_new_item'          => __( 'Add New Service', 'naveed' ),
        'add_new'               => __( 'Add New Service', 'naveed' ),
        'new_item'              => __( 'New Service', 'naveed' ),
        'edit_item'             => __( 'Edit Service', 'naveed' ),
        'update_item'           => __( 'Update Service', 'naveed' ),
        'view_item'             => __( 'View Service', 'naveed' ),
        'search_items'          => __( 'Search Service', 'naveed' ),
        'not_found'             => __( 'Not found', 'naveed' ),
        'not_found_in_trash'    => __( 'Not found in Trash', 'naveed' ),
        'featured_image'        => __( 'Featured Image', 'naveed' ),
        'set_featured_image'    => __( 'Set featured image', 'naveed' ),
        'remove_featured_image' => __( 'Remove featured image', 'naveed' ),
        'use_featured_image'    => __( 'Use as featured image', 'naveed' ),
        'insert_into_item'      => __( 'Insert into item', 'naveed' ),
        'uploaded_to_this_item' => __( 'Uploaded to this item', 'naveed' ),
        'items_list'            => __( 'Items list', 'naveed' ),
        'items_list_navigation' => __( 'Items list navigation', 'naveed' ),
        'filter_items_list'     => __( 'Filter items list', 'naveed' ),
    );
    $args = array(
        'label'                 => __( 'Service', 'naveed' ),
        'description'           => __( 'A post type for your services', 'naveed' ),
        'labels'                => $labels,
        'supports'              => array( 'title', 'editor', 'excerpt', 'thumbnail' ),
        'taxonomies'            => array( 'naveed_service_category' ),
        'hierarchical'          => false,
        'public'                => true,
        'show_ui'               => true,
        'show_in_menu'          => true,
        'menu_position'         => 26,
        'menu_icon'             => 'dashicons-screenoptions',
        'show_in_admin_bar'     => true,
        'show_in_nav_menus'     => true,
        'can_export'            => true,
        'has_archive'           => true,        
        'exclude_from_search'   => false,
        'publicly_queryable'    => true,
        'capability_type'       => 'page',
        'rewrite'               => array( 'slug' => $slug ),        
    );
    register_post_type( 'services', $args );
}
add_action( 'init', 'naveed_register_services', 0 );

//-----------------------------------------------------
// naveed_service_category
// Register the Taxonomy for the Services
//-----------------------------------------------------

if ( ! function_exists( 'naveed_service_category' ) ) {
// Register Custom Taxonomy
    function naveed_service_category() {
        $labels = array(
            'name'                       => _x( 'Categories', 'Taxonomy General Name', 'naveed' ),
            'singular_name'              => _x( 'Category', 'Taxonomy Singular Name', 'naveed' ),
            'menu_name'                  => __( 'Categories', 'naveed' ),
            'all_items'                  => __( 'All Categories', 'naveed' ),
            'parent_item'                => __( 'Parent Category', 'naveed' ),
            'parent_item_colon'          => __( 'Parent Category:', 'naveed' ),
            'new_item_name'              => __( 'New Category Name', 'naveed' ),
            'add_new_item'               => __( 'Add New Category', 'naveed' ),
            'edit_item'                  => __( 'Edit Category', 'naveed' ),
            'update_item'                => __( 'Update Category', 'naveed' ),
            'separate_items_with_commas' => __( 'Separate Categories with commas', 'naveed' ),
            'search_items'               => __( 'Search Categories', 'naveed' ),
            'add_or_remove_items'        => __( 'Add or remove Category', 'naveed' ),
            'choose_from_most_used'      => __( 'Choose from the most Categories', 'naveed' ),
            'not_found'                  => __( 'Not Found', 'naveed' ),
        );
        $rewrite = array(
            'slug'                       => 'service-category',
            'with_front'                 => true,
            'hierarchical'               => false,
        );
        $args = array(
            'labels'                     => $labels,
            'hierarchical'               => true,
            'public'                     => true,
            'show_ui'                    => true,
            'show_in_rest'               => true,
            'show_admin_column'          => true,
            'show_in_nav_menus'          => true,
            'show_tagcloud'              => true,
            'rewrite'                    => $rewrite,
        );
        register_taxonomy( 'service-category', array( 'services' ), $args );
    }

// Hook into the 'init' services
    add_action( 'init', 'naveed_service_category', 0 );
}

To change place holder of title use the following code.

// Change the post title to request the name of the services
 add_filter( 'enter_title_here', function( $title ) {
      $screen = get_current_screen();
      if  ( 'services' == $screen->post_type ) {
           $title = 'Service Title';
      }
      return $title;
 });

In next post we will show you how to create Metabox in WordPress

Previous Post Next Post

8 Comments

  • Francisca says:

    Thank you for sharing your info. I really appreciate your efforts and I am waiting for your further write ups thank you
    once again.

    Feel free to visit my site: Twicsy

  • Ouida says:

    I don’t even know the way I finished up right here, but I thought this post was once good.
    I do not recognize who you might be however certainly you’re going
    to a famous blogger should you aren’t already. Cheers!

    my site: why won’t instagram let me view my followers

  • QuillaCiz says:

    buy lasix However, the expression of ERbeta in human bladder cancer has not been thoroughly investigated

  • Odette says:

    I’m really impressed with your writing skills as well as with the layout
    on your weblog. Is this a paid theme or did you customize it yourself?
    Either way keep up the excellent quality writing, it’s rare to see a great
    blog like this one nowadays.

  • Sallie says:

    I am really enjoying the theme/design of your weblog.

    Do you ever run into any internet browser compatibility issues?
    A small number of my blog visitors have complained about my site not working correctly in Explorer but looks great in Opera.

  • Alba says:

    This is very interesting, You are a very skilled blogger.

    I’ve joined your feed and look forward to seeking more of
    your wonderful post. Also, I’ve shared your site
    in my social networks!

  • Jett says:

    free online dating sites with no fees mature adult phone chat free dating site for usa completely free local dating sites

  • cvyfrnlwjy says:

    Create Custom Post Type in WordPress – Naveed

Leave a Reply

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