Skip to content

Instantly share code, notes, and snippets.

@flegfleg
Created February 2, 2019 11:22
Show Gist options
  • Save flegfleg/2ac283d05d1706f818404b24cac6472a to your computer and use it in GitHub Desktop.
Save flegfleg/2ac283d05d1706f818404b24cac6472a to your computer and use it in GitHub Desktop.
add caps to wp custom post types
<?php
function create_post_type_items() {
register_post_type( 'acme_item',
array(
'labels' => array(
'name' => __( 'CB2 Items' ),
'singular_name' => __( 'CB2 Item' )
),
'public' => true,
'has_archive' => true,
'capabilities' => array(
'edit_post' => 'edit_item',
'edit_posts' => 'edit_items',
'edit_others_posts' => 'edit_other_items',
'publish_posts' => 'publish_items',
'read_post' => 'read_item',
'read_private_posts' => 'read_private_items',
'delete_post' => 'delete_item'
),
// adding map_meta_cap will map the meta correctly
'map_meta_cap' => true
)
);
}
function create_post_type_locations() {
register_post_type( 'acme_location',
array(
'labels' => array(
'name' => __( 'CB2_Locations' ),
'singular_name' => __( 'CB2 Location' )
),
'public' => true,
'has_archive' => true,
'capabilities' => array(
'edit_post' => 'edit_location',
'edit_posts' => 'edit_locations',
'edit_others_posts' => 'edit_other_locations',
'publish_posts' => 'publish_locations',
'read_post' => 'read_location',
'read_private_posts' => 'read_private_locations',
'delete_post' => 'delete_location'
),
// adding map_meta_cap will map the meta correctly
'map_meta_cap' => true
)
);
}
add_action( 'init', 'create_post_type_items' );
add_action('init', 'create_post_type_locations');
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment