This little plugin shows the user role 'author' only its own categories & posts. So you can edit it for your own post-type and taxonomy.
/*
Plugin Name: Show own categories
Description: This plugin shows the user role 'author' only its own categories & posts
Version: 0.1
Author: Soren Wrede
License: GPL2
License URI: https://www.gnu.org/licenses/gpl-2.0.html
*/
class SW_Category_Restriction{
private $user_cats = NULL;
public function __construct(){
//Save author ID for category
add_action( 'create_category', array( &$this, 'save_category_author' ) );
//Set manage_categories cap for 'Author'
add_action( 'admin_init', array( &$this, 'add_author_cap_categories' ) );
//Remove manage_categories capfor 'Author'
register_deactivation_hook( __FILE__, array( &$this, 'remove_author_cap_categories' ) );
//Filter categorys in new-post, edit-post, edit-categories
add_action( 'admin_print_scripts-post-new.php', array( &$this, 'filter_post_page' ) );
add_action( 'admin_print_scripts-post.php', array( &$this, 'filter_post_page' ) );
add_action( 'admin_print_scripts-edit-tags.php', array( &$this, 'filter_post_page' ) );
//just show own posts
global $pagenow;
if ( $pagenow == 'edit.php' )
add_filter( 'pre_get_posts', array( &$this, 'filter_edit_page' ) );
}
public function save_category_author( $term_id ) {
$user_id = get_current_user_id();
add_term_meta( $term_id, 'author', $user_id );
}
public function get_user_cats( $user_id ) {
$args = [
'hide_empty' => false,
'meta_query' => [
[
'key' => 'author',
'value' => $user_id,
]
]
];
$terms = get_terms( 'category', $args );
$ids = '-1,';
foreach ($terms as $term ) {
$ids .= $term->term_id . ',';
}
$ids = substr($ids, 0, -1);
$this->user_cats = $ids;
}
public function add_author_cap_categories() {
$role = get_role( 'author' );
$role->add_cap( 'manage_categories' );
}
public function remove_author_cap_categories() {
$role = get_role( 'author' );
$role->remove_cap( 'manage_categories' );
}
public function exclusions( $exclusions ){
$exclusions .= " AND ( t.term_id IN ( $this->user_cats ) OR tt.taxonomy NOT IN ( 'category' ) )";
return $exclusions;
}
public function filter_post_page() {
//check post-type
$screen = get_current_screen();
if ( $screen->post_type != 'post' )
return;
//just Author and lower
if ( current_user_can('delete_others_posts') )
return;
$user_id = get_current_user_id();
$this->get_user_cats( $user_id );
if ( empty( $this->user_cats ) )
return;
add_filter( 'list_terms_exclusions', array( &$this, 'exclusions' ) );
}
public function filter_edit_page( $query ) {
if ( current_user_can('delete_others_posts') )
return;
$query->set( 'author', get_current_user_id() );
return $query;
}
}
new SW_Category_Restriction();
create_term
or a similar hook - save the author ID asterm_meta
and base your conditionals off that. I'm not sure if it's the best solution but it's a starting point.