WordPress – Register a Custom Post Type

WordPress - General Custom Post Types Functions PHP

Snippet Overview

WordPress – Register a Custom Post Type

As you know, by default, WordPress comes with two post types: page and post. If you want a more variety of suitable post types to choose from, e.g., a post type for products, you have to create them yourself. Such post types are called Custom Post Types.

  • The $labels parameter is insignificant, you don’t have to pay too much attention to it. But, it will help you recognize the post type in the admin area.
  • The $supports parameter is used for declaring that the post type is supported with title, editor, excerpt, featured image, etc.
  • The $args parameter is used for including all the above arrays and some other important arguments as well.

 

function prefix_create_custom_post_type() {
    /* The $labels describes how the post type appears. */
    $labels = array(
        'name'                => 'Products',                         // This is the plural title of the group
        'singular_name'       => 'Product'                           // This is the singular title of the group
        'all_items'           => 'All Products',                     // the all items menu item
        'add_new'             => 'Add New',                          // The add new menu item
        'add_new_item'        => 'Add New Product',                  // Add New Display Title
        'edit'                => 'Edit',                             // Edit Dialog
        'edit_item'           => 'Edit Product',                     // Edit Display Title
        'new_item'            => 'New Product',                      // New Display Title
        'view_item'           => 'View Product',                     // View Display Title
        'search_items'        => 'Search Products',                  // Search Custom Type Title
        'not_found'           => 'Nothing found in the Database.',   // This displays if there are no entries yet
        'not_found_in_trash'  => 'Nothing found in Trash',           // This displays if there is nothing in the trash
        'parent_item_colon'   => ''
    );

    /* The $supports parameter describes what the post type supports */
    $supports = array(
        'title',              // Post title
        'editor',             // Post content
        'excerpt',            // Allows short description
        'author',             // Allows showing and choosing author
        'thumbnail',          // Allows feature images
        'comments',           // Enables comments
        'trackbacks',         // Supports trackbacks
        'revisions',          // Shows autosaved version of the posts
        'custom-fields'       // Supports by custom fields
    );

    /* The $args parameter holds important parameters for the custom post type */
    $args = array(
        'labels'              => $labels,
        'description'         => 'Post type post product',         // Description
        'supports'            => $supports,
        'taxonomies'          => array( 'category', 'post_tag' ),  // Allowed taxonomies
        'hierarchical'        => false,                            // Allows hierarchical categorization, false behaves like Post, true behaves like a Page
        'public'              => true,                             // Makes the post type public
        'show_ui'             => true,                             // Displays an interface for this post type
        'show_in_menu'        => true,                             // Displays in the Admin Menu (the left panel)
        'show_in_nav_menus'   => true,                             // Displays in Appearance -> Menus
        'show_in_admin_bar'   => true,                             // Displays in the black admin bar
        'menu_position'       => 5,                                // The position number in the left menu
        'menu_icon'           => true,                             // The URL for the icon used for this post type
        'menu_icon'           => 'dashicons-media-default',        // the icon for the custom post type menu. uses built-in dashicons (CSS class name)
        'can_export'          => true,                             // Allows content export using Tools -> Export
        'has_archive'         => true,                             // Enables post type archive (by month, date, or year)
        'exclude_from_search' => false,                            // Excludes posts of this type in the front-end search result page if set to true, include them if set to false
        'query_var'           => true,                             // 
        'publicly_queryable'  => true,                             // Allows queries to be performed on the front-end part if set to true
        'capability_type'     => 'post',                           // Allows read, edit, delete like “Post”
        'rewrite'             => array( 'slug' => 'products',      // Allows specification of slug name
        'with_front'          => false ),                          // Allow specification of url slug
    );

    register_post_type('product', $args); //Create a post type with the slug is ‘product’ and arguments in $args.
}
add_action('init', 'prefix_create_custom_post_type');

Source: https://metabox.io/create-wordpress-custom-post-types/

0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply