I want to restrict which post appears in a user edit screen based on its role. The roles are mapped to category.

  1. I am trying to add some conditions at the time posts are retrieved so that only posts belonging to a specific category are shown in the edit screen.

  2. I am not able to find out the appropriate hook to fire. I tried 'pre_get_posts' and then try to modify the query which satisfies the condition if (is_admin() && $query->is_main_query()) { but to no use.

1 Answer 1


I'm not sure why you're having trouble with pre_get_posts as that is the correct hook to use. For example, if we wanted to restrict the edit.php screen to only show posts from authors when the current user is an author we could say:

 * Pre Get Posts
 * Restrictions by Role
 * @return void
function wpse378774_pgp( $query ) {
    // Return Early - Not Admin Panel
    if( ! is_admin() ) {
    // Return Early - Not Main Query
    } else if( ! $query->is_main_query() ) {
    $user = wp_get_current_user();
    // Return Early - User not an author
    if( ! in_array( 'author', (array)$user->roles ) ) {
    // Limit all queries to only show posts by author
    if( $query->is_main_query() ) {
        // Get all user IDs who are authors
        $user_query = new WP_User_Query( array(
            'role'      => 'author',
            'fields'    => 'ID',
        ) );
        $user_ids = $user_query->get_results();
        // Set our query modifier
        $query->set( 'author__in', $user_ids );
add_action( 'pre_get_posts', 'wpse378774_pgp' );

For more information please review WP_Query and WP_User_Query.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.