Welcome to Findnerd. Today we are going to discuss the wordpress function register_post_type. If you are working on word-press then you should be familiar with different post types in word-press like post,page,revision,draft etc. Word-press stores the all post types in wp_posts table. Here we are trying to create a new post type for different purposes. You need to assign the proper name to post type. If you put the dashes in your post type name then you can not add new columns in this post type. Name should not contain the word-press related terms like post,theme,query_post etc. You can assign the different names such as Companies,Staffing etc.
You need to know that you must invoke the register_post_type by using init action. it will not work when it invokes before and after the init hook so the best way to invoke this function is init hook. If you invokes it after the init then it will be work properly. You can put it in plugin files if you want to apply it on all themes. You can put in functions.php for specific themes. I am going to explain its parameter to better understanding. There are two primary parameters. one is post type name and array of arguments. Please have a look.
Parameters |
Description |
post_type(string) |
This is required string. It can contain maximum 20 characters and spaces, capital letters are not allowed. |
label(string) |
It is optional one and it is a plural descriptive to post type. |
labels(array) |
Useful to set the labels for different sections.It is optional and it is in array form with different elements such as name, singular_name,add_new,add_new_item,edit_item,new_item,view_item,'search_items,not_found,not_found_in_trash,parent_item_colon,all_items,archives
,insert_into_item,uploaded_to_this_item,featured_image,set_featured_image,remove_featured_image,use_featured_image,
menu_name,filter_items_list,items_list_navigation,items_list,name_admin_bar.
|
description(string) |
It is also optional but useful to describe the post type. |
public(boolean) |
its control the visibility of the post type for the different users and it is optional one, default is set to be false. |
exclude_from_search(boolean) |
Useful to exclude the post type from front end search. If public argument is set to true then it should be false. |
publicly_queryable(boolean) |
Useful to set whether queries can be invoke or not on front end. Its default value is same as public argument. |
show_ui(boolean) |
Useful to set the default ui to show the post type in admin. Default value is same as the public argument. |
show_in_nav_menus |
Useful to set the post type in navigation menu or not. Default value is same as public argument. |
show_in_menu(boolean/string) |
Useful to set option to show in admin menu. It is required show_ui to true. If we set the value 'tools.php?' then post type will be set as the submenu of it. |
show_in_admin_bar(boolean) |
Useful to set the option to display then post type in word-press admin bar. |
menu_position(integer) |
Useful to set the order the menu in admin menu. Argument show_in_menu should be true. Default order is below the comment. Please check the values for different orders.
5 - below Posts
10 - below Media
15 - below Links
20 - below Pages
25 - below comments
60 - below first separator
65 - below Plugins
70 - below Users
75 - below Tools
80 - below Settings
100 - below second separator
|
menu_icon(string) |
It is also optional one. You need to pass the image url to set the menu icon. |
capability_type(string/array) |
This is optional one. The string set the option to build the read, edit, and delete capabilities. |
capabilities(array) |
You can set the array with different capabilities.
'capability_type' => array('company',companies);
'capabilities' => array(
'edit_post' => 'edit_company',
'read_post' => 'read_company',
'delete_post' => 'delete_company',
'edit_posts' => 'edit_companies',
'edit_others_posts' => 'edit_others_companies',
'publish_posts' => 'publish_companies',
'read_private_posts' => 'read_private_companies',
'create_posts' => 'edit_companies',
),
|
map_meta_cap(boolean) |
Useful to set the internal default meta capability handling. With false value, standard admin role can't modify the post type.
Then the edit_post capability must be added to all roles to add or edit the posts types.
|
hierarchical(boolean) |
This grants the post type to specify the parent. 'supports' argument is set to 'page-attributes' to enable the parent select box on edit screen. |
supports(boolean/array) |
If set to false then it hides the default options like title editor etc and you can also build of default options to active
such as custom-fields,title,editor etc.
|
register_meta_box_cb(callback) |
Callback function to called when settings up the meta boxes. |
taxonomies(array) |
Useful to enable the default taxonomies like categories,page_tags etc via an array. |
has_archive(string/boolean) |
Enable the post type archive. |
rewrite(array/boolean) |
Useful to handling of rewrites for this post type |
query_var(boolean/array) |
Default set to $post_type and can make changes if required. |
can_export(boolean) |
Enable/disable the export option of post type. Default is set to true. |
You can check different options available in this function. Now we are going explain the small example. Please have a look.
add_action( 'init', 'create_company');
function create_company() {
$labels = array(
'name' => _x( 'Companies', 'post type general name', 'your-plugin-textdomain' ),
'singular_name' => _x( 'Companies', 'post type singular name', 'your-plugin-textdomain' ),
'menu_name' => _x( 'Companies', 'admin menu', 'your-plugin-textdomain' ),
'name_admin_bar' => _x( 'Company', 'add new on admin bar', 'your-plugin-textdomain' ),
'add_new' => _x( 'Add New', 'product', 'your-plugin-textdomain' ),
'add_new_item' => __( 'Add New Company', 'your-plugin-textdomain' ),
'new_item' => __( 'New Company', 'your-plugin-textdomain' ),
'edit_item' => __( 'Edit Company', 'your-plugin-textdomain' ),
'view_item' => __( 'View Company', 'your-plugin-textdomain' ),
'all_items' => __( 'All Companies', 'your-plugin-textdomain' ),
'search_items' => __( 'Search Companies', 'your-plugin-textdomain' ),
'parent_item_colon' => __( 'Parent Companies:', 'your-plugin-textdomain' ),
'not_found' => __( 'No Companies found.', 'your-plugin-textdomain' ),
'not_found_in_trash' => __( 'No Companies found in Trash.', 'your-plugin-textdomain' )
);
$args = array(
'labels' => $labels,
'description' => __( 'Description.', 'your-plugin-textdomain' ),
'public' => true,
'menu_icon' => 'dashicons-company',
'publicly_queryable' => true,
'show_ui' => true,
'show_in_menu' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'company' ),
'capability_type' => 'post',
'has_archive' => true,
'hierarchical' => false,
'menu_position' => null,
'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments' )
);
register_post_type( 'company', $args );
}
0 Comment(s)