Skip to content

Instantly share code, notes, and snippets.

@websitecareio
Created November 9, 2021 22:09
Show Gist options
  • Save websitecareio/4448220caaef3b3bb265794594aa95b1 to your computer and use it in GitHub Desktop.
Save websitecareio/4448220caaef3b3bb265794594aa95b1 to your computer and use it in GitHub Desktop.
Limit WordPress posts by author.
add_filter('pre_get_posts', 'posts_for_current_author');
function posts_for_current_author($query) {
if(!is_admin()) { return $query; }
global $pagenow;
if( 'upload.php' == $pagenow || !$query->is_admin ) {
if( !current_user_can( 'edit_others_posts' ) ) {
global $user_ID;
$query->set('author', $user_ID );
}
}
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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment