WordPress restrict access to custom post types for user roles

If you’re creating custom post types to wordpress site sometimes you need to restrict access to custom post types from back-end to other users. For a example if you created the custom post type named “Campaigns” and you want to enable that “Campaigns” page from admin panel only administrator user role.To do this I’ll show very simple method by explaining from start to end.Hope every users can understand easily.
Let’s start from adding custom post types named “Campaigns”.

/* register the "campaigns" post type */
add_action( 'init', 'wp_link_index_post_type' );
function wp_link_index_post_type() {
    $args = array(
                  'description' => 'Campaigns',
                  'show_ui' => true,
                  'menu_position' => 4,
				  'menu_icon' => 'dashicons-format-aside',
                  'exclude_from_search' => true,
                  'labels' => array(
                                    'name'=> 'Campaigns',
                                    'singular_name' => 'Campaigns',
                                    'add_new' => 'Add Campaign',
                                    'add_new_item' => 'Add Campaign',
                                    'edit' => 'Edit Campaigns',
                                    'edit_item' => 'Edit Campaigns',
                                    'new-item' => 'New Campaign',
                                    'view' => 'View Campaigns',
                                    'view_item' => 'View Campaigns',
                                    'search_items' => 'Search Campaigns',
                                    'not_found' => 'No Campaigns Found',
                                    'not_found_in_trash' => 'No Campaigns Found in Trash',
                                    'parent' => 'Parent Campaigns'
                                   ),
                 'public' => true,
                 'capability_type' => 'post',
                 'hierarchical' => false,
                 'rewrite' => true,
                 'supports' => array('title', 'editor', 'excerpt', 'thumbnail', 'custom-fields', 'comments'),
				 'has_archive' => true
                 );
    register_post_type( 'campaigns' , $args );
}

If we added above code all users can see it after them logged into their dashboard.Subscribers can view that too. 🙂
To restrict access to custom post type you can send parameters via “capability_type” as following.

'capability_type' => 'post',
					 'capabilities' => array( // allow only to admin
						'publish_posts' => 'manage_options',
						'edit_posts' => 'manage_options',
						'edit_others_posts' => 'manage_options',
						'delete_posts' => 'manage_options',
						'delete_others_posts' => 'manage_options',
						'read_private_posts' => 'manage_options',
						'edit_post' => 'manage_options',
						'delete_post' => 'manage_options',
						'read_post' => 'manage_options',
					),

Final fully code is like this.

/* register the "campaigns" post type */
add_action( 'init', 'wp_link_index_post_type' );
function wp_link_index_post_type() {
    $args = array(
                  'description' => 'Campaigns',
                  'show_ui' => true,
                  'menu_position' => 4,
				  'menu_icon' => 'dashicons-format-aside',
                  'exclude_from_search' => true,
                  'labels' => array(
                                    'name'=> 'Campaigns',
                                    'singular_name' => 'Campaigns',
                                    'add_new' => 'Add Campaign',
                                    'add_new_item' => 'Add Campaign',
                                    'edit' => 'Edit Campaigns',
                                    'edit_item' => 'Edit Campaigns',
                                    'new-item' => 'New Campaign',
                                    'view' => 'View Campaigns',
                                    'view_item' => 'View Campaigns',
                                    'search_items' => 'Search Campaigns',
                                    'not_found' => 'No Campaigns Found',
                                    'not_found_in_trash' => 'No Campaigns Found in Trash',
                                    'parent' => 'Parent Campaigns'
                                   ),
                 'public' => true,
                 'capability_type' => 'post',
					 'capabilities' => array( // allow only to admin
						'publish_posts' => 'manage_options',
						'edit_posts' => 'manage_options',
						'edit_others_posts' => 'manage_options',
						'delete_posts' => 'manage_options',
						'delete_others_posts' => 'manage_options',
						'read_private_posts' => 'manage_options',
						'edit_post' => 'manage_options',
						'delete_post' => 'manage_options',
						'read_post' => 'manage_options',
					),
                 'hierarchical' => false,
                 'rewrite' => true,
                 'supports' => array('title', 'editor', 'excerpt', 'thumbnail', 'custom-fields', 'comments'),
				 'has_archive' => true
                 );
    register_post_type( 'campaigns' , $args );
}

Then this campaign page viewable for administrator user privileges only.
Hope this would be help.Comment here if you have any problems regarding this.
Thanks