Having trouble with how to filter the post listing from WordPress back-end base on something else like user role? Read this!
Sometime you might have custom role, let’s say co-editor, setup using members plugin in your WordPress. And then you want to achieve showing all posts only to the user who has administrator role but user with co-editor role sees only some of the posts. You could use the below code to overcome this saturation.
function custom_post_where( $where ) {
if( is_admin() && ( !current_user_can('administrator') ) && ( current_user_can('co_editor') ) ) {
// get current user
global $current_user;
get_currentuserinfo();
$user_id = $current_user->ID;
// you can add your own query here, something like this
$where .= " AND ID IN (SELECT ID FROM $wpdb->posts WHERE post_author = $user_id)";
}
return $where;
}
// plug to this filter
add_filter( 'posts_where', 'custom_post_where' );