WordPress by default shows ALL posts to users who are Authors. I wanted Authors to only be able to see their own posts so I came up with the following code which works well.
Any suggestions to condense/optimize it?
// Only show posts related to current author
add_action('pre_get_posts', 'query_set_only_author' );
function query_set_only_author( $wp_query ) {
global $current_user;
if ( is_admin() && !current_user_can('edit_others_posts') ) {
$wp_query->set( 'author', $current_user->ID );
}
}
// Check that user is not an Administrator
if (!current_user_can('promote_users')) {
add_filter('views_edit-post', 'remove_mine');
add_filter('views_edit-post', 'posts_all');
add_filter('views_edit-post', 'posts_published');
add_filter('views_edit-post', 'posts_draft');
add_filter('views_edit-post', 'posts_pending');
add_filter('views_edit-post', 'posts_trash');
}
// Remove "Mine" from filter bar
function remove_mine( $views ) {
unset($views['mine']);
return $views;
}
// Fix "All" post count to only include posts related to current author
function posts_all($views) {
global $current_user;
$query = array(
'author' => $current_user->ID,
'post_type' => 'post'
);
$result = new WP_Query($query);
if ( get_query_var('post_status') == NULL ) $class = ' class="current"';
$link = sprintf(__('<a href="%s"'.$class.'>All <span class="count">(%d)</span></a>', 'all'),
admin_url('edit.php?post_type=post'),
$result->found_posts);
$views['all'] = $link;
return $views;
}
// Fix "Published" post count to only include posts related to current author
function posts_published($views) {
global $current_user;
$query = array(
'author' => $current_user->ID,
'post_status' => 'publish',
'post_type' => 'post'
);
$result = new WP_Query($query);
if ( get_query_var('post_status') == 'publish' ) $class = ' class="current"';
$link = sprintf(__('<a href="%s"'.$class.'>Published <span class="count">(%d)</span></a>', 'publish'),
admin_url('edit.php?post_status=publish&post_type=post'),
$result->found_posts);
$views['publish'] = $link;
return $views;
}
// Fix "Drafts" post count to only include posts related to current author
function posts_draft($views) {
global $current_user;
$query = array(
'author' => $current_user->ID,
'post_status' => 'draft',
'post_type' => 'post'
);
$result = new WP_Query($query);
if ( get_query_var('post_status') == 'draft' ) $class = ' class="current"';
$link = sprintf(__('<a href="%s"'.$class.'>Draft'.((sizeof($result->posts)>1)?"s":"").' <span class="count">(%d)</span></a>', 'draft'),
admin_url('edit.php?post_status=draft&post_type=post'),
$result->found_posts);
$views['draft'] = $link;
return $views;
}
// Fix "Pending" post count to only include posts related to current author
function posts_pending($views) {
global $current_user;
$query = array(
'author' => $current_user->ID,
'post_status' => 'pending',
'post_type' => 'post'
);
$result = new WP_Query($query);
if ( get_query_var('post_status') == 'pending' ) $class = ' class="current"';
$link = sprintf(__('<a href="%s"'.$class.'>Pending <span class="count">(%d)</span></a>', 'pending'),
admin_url('edit.php?post_status=pending&post_type=post'),
$result->found_posts);
$views['pending'] = $link;
return $views;
}
// Fix "Trash" post count to only include posts related to current author
function posts_trash($views) {
global $current_user;
$query = array(
'author' => $current_user->ID,
'post_status' => 'trash',
'post_type' => 'post'
);
$result = new WP_Query($query);
if ( get_query_var('post_status') == 'trash' ) $class = ' class="current"';
$link = sprintf(__('<a href="%s"'.$class.'>Trash <span class="count">(%d)</span></a>', 'trash'),
admin_url('edit.php?post_status=trash&post_type=post'),
$result->found_posts);
$views['trash'] = $link;
return $views;
}
Ok I figured it out. Here’s the optimized code: