Skip to content

Instantly share code, notes, and snippets.

@rowej83
Last active December 5, 2017 17:45
Show Gist options
  • Save rowej83/8d09de3ca5b01b1a74fbcb85f4cde5fa to your computer and use it in GitHub Desktop.
Save rowej83/8d09de3ca5b01b1a74fbcb85f4cde5fa to your computer and use it in GitHub Desktop.
WordPress Plugin so that client-event role users can only see their posts.
<?php
/*
Plugin Name: View only your posts
Description:Small plugin so that client-event users can only see their posts.
Author: Jason Rowe
*/
function posts_for_current_author($query) {
global $pagenow;
$tempuser = wp_get_current_user();
if( 'edit.php' != $pagenow || !$query->is_admin )
return $query;
if( in_array( 'client-event', (array) $tempuser->roles ) ) {
add_action( 'views_edit-post', 'child_remove_some_post_views' );
global $user_ID;
$query->set('author', $user_ID );
}
return $query;
}
function child_remove_some_post_views( $views ) {
unset($views['all']);
unset($views['publish']);
unset($views['trash']);
unset($views['draft']);
unset($views['pending']);
return $views;
}
add_filter('pre_get_posts', 'posts_for_current_author');
// Below is example for a specific right check
// credit goes to http://www.wpbeginner.com/plugins/how-to-limit-authors-to-their-own-posts-in-wordpress-admin/
// function posts_for_current_author($query) {
// global $pagenow;
// if( 'edit.php' != $pagenow || !$query->is_admin )
// return $query;
// if( !current_user_can( 'edit_others_posts' ) ) {
// global $user_ID;
// $query->set('author', $user_ID );
// }
// return $query;
// }
// add_filter('pre_get_posts', 'posts_for_current_author');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment