php - Wordpress custom user roles edit others posts?
Get the solution ↓↓↓I'm looking to create custom user roles in WP - however I'm experiencing an issue where users of the same group can't edit each others posts. Below is the code I have in functions.php:
function userEngineer() {
add_role('engineer', 'Engineer');
$role = get_role('engineer');
$role->add_cap( 'read' );
$role->add_cap( 'moderate_comments' );
$role->add_cap( 'manage_categories' );
$role->add_cap( 'manage_links' );
$role->add_cap( 'upload_files' );
$role->add_cap( 'edit_posts' );
$role->add_cap( 'edit_others_posts' );
$role->add_cap( 'edit_published_posts' );
$role->add_cap( 'publish_posts' );
$role->add_cap( 'edit_pages' );
$role->add_cap( 'edit_others_pages' );
// allowed
$role->add_cap( 'read_engineering');
$role->add_cap( 'edit_engineering' );
$role->add_cap( 'edit_others_engineering' );
$role->add_cap( 'delete_engineering' );
// restrictions (posts)
$role->add_cap('read_webinar');
$role->add_cap('edit_webinars', false);
$role->add_cap('delete_webinar', false);
$role->add_cap('read_events');
$role->add_cap('edit_events', false);
$role->add_cap('delete_events', false);
$role->add_cap('read_research');
$role->add_cap('edit_researchs', false);
$role->add_cap('edit_researchs', false);
$role->add_cap('delete_researchs', false);
// restrictions (general)
$role->add_cap( 'edit_themes', false );
$role->add_cap( 'switch_themes', false );
$role->add_cap( 'edit_files', false );
$role->add_cap( 'edit_theme_options', false );
$role->add_cap( 'install_plugins', false );
$role->add_cap( 'edit_plugins', false );
}
// Marketing Tier 1
function userMarketingOne() {
add_role('marketing-tier-1', 'Marketing Tier 1');
$role = get_role('marketing-tier-1');
$role->add_cap( 'read' );
$role->add_cap( 'moderate_comments' );
$role->add_cap( 'manage_categories' );
$role->add_cap( 'manage_links' );
$role->add_cap( 'upload_files' );
$role->add_cap( 'edit_posts' );
$role->add_cap( 'edit_others_posts' );
$role->add_cap( 'edit_published_posts' );
$role->add_cap( 'publish_posts' );
$role->add_cap( 'edit_pages' );
// restrictions
$role->add_cap( 'edit_themes', false );
$role->add_cap( 'read_engineering');
$role->add_cap( 'edit_engineering', false );
$role->add_cap( 'delete_engineering', false );
// restrictions (general)
$role->add_cap( 'edit_themes', false );
$role->add_cap( 'switch_themes', false );
$role->add_cap( 'edit_files', false );
$role->add_cap( 'edit_theme_options', false );
$role->add_cap( 'install_plugins', false );
$role->add_cap( 'edit_plugins', false );
}
// Marketing Tier 2
function userMarketingTwo() {
add_role('marketing-tier-2', 'Marketing Tier 2');
$role = get_role('marketing-tier-2');
$role->add_cap( 'read' );
$role->add_cap( 'manage_links' );
$role->add_cap( 'upload_files' );
$role->add_cap( 'edit_posts' );
$role->add_cap( 'edit_others_posts' );
$role->add_cap( 'edit_published_posts' );
$role->add_cap( 'publish_posts' );
$role->add_cap( 'edit_pages' );
// restrictions
$role->add_cap( 'edit_themes', false );
$role->add_cap('read_webinar');
$role->add_cap('edit_webinars', false);
$role->add_cap('delete_webinar', false);
$role->add_cap('read_events');
$role->add_cap('edit_events', false);
$role->add_cap('delete_events', false);
$role->add_cap('read_research');
$role->add_cap('edit_research', false);
$role->add_cap('delete_research', false);
$role->add_cap( 'read_engineering');
$role->add_cap( 'edit_engineering', false );
$role->add_cap( 'delete_engineering', false );
// restrictions (general)
$role->add_cap( 'edit_themes', false );
$role->add_cap( 'switch_themes', false );
$role->add_cap( 'edit_files', false );
$role->add_cap( 'edit_theme_options', false );
$role->add_cap( 'install_plugins', false );
$role->add_cap( 'edit_plugins', false );
}
add_action('admin_init', 'userEngineer');
add_action('admin_init', 'userMarketingOne');
add_action('admin_init', 'userMarketingTwo');
I was under the impression that$role->add_cap( 'edit_others_engineering' );
would allow for this, per this link https://wordpress.org/support/article/roles-and-capabilities/#edit_others_posts - for custom post types, do I have to do something else?
the admin can't edit the other users custom post type
Based on this and many others I'm under the impression it's just the edit_others function, but this one is doing a loop and I'm confused as to why.
How do you set it so people in a custom user group can edit each others posts?
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: using $this when not in object context laravel
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.