How to create custom post in WordPress?

To register custom post type in WordPress you need to use register_post_type method.

You should invoke this method under init hook.


add_action( 'init', 'prouduct_init' );
function product_init() {
$labels = array(
'name'               => _x( 'Products', 'post type general name', 'your-plugin-textdomain' ),
'singular_name'      => _x( 'Product', 'post type singular name', 'your-plugin-textdomain' ),
'menu_name'          => _x( 'Products', 'admin menu', 'your-plugin-textdomain' ),
'name_admin_bar'     => _x( 'Product', 'add new on admin bar', 'your-plugin-textdomain' ),
'add_new'            => _x( 'Add New', 'Product', 'your-plugin-textdomain' ),
'add_new_item'       => __( 'Add New Product', 'your-plugin-textdomain' ),
'new_item'           => __( 'New Product', 'your-plugin-textdomain' ),
'edit_item'          => __( 'Edit Product', 'your-plugin-textdomain' ),
'view_item'          => __( 'View Product', 'your-plugin-textdomain' ),
'all_items'          => __( 'All Products', 'your-plugin-textdomain' ),
'search_items'       => __( 'Search Products', 'your-plugin-textdomain' ),
'parent_item_colon'  => __( 'Parent Products:', 'your-plugin-textdomain' ),
'not_found'          => __( 'No Products found.', 'your-plugin-textdomain' ),
'not_found_in_trash' => __( 'No Products found in Trash.', 'your-plugin-textdomain' )
);

$args = array(
'labels'             => $labels,
'description'        => __( 'Description.', 'your-plugin-textdomain' ),
'public'             => true,
'publicly_queryable' => true,
'show_ui'            => true,
'show_in_menu'       => true,
'query_var'          => true,
'rewrite'            => array( 'slug' => 'Product' ),
'capability_type'    => 'post',
'has_archive'        => true,
'hierarchical'       => false,
'menu_position'      => null,
'supports'           => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments' )
);
register_post_type( 'product', $args );
}

Leave a Reply