How pre_get_posts filter by roles in WP Admin

I’d make 2 queries. This would look something like this :

function add_role_filter_to_posts_administration(){

    //execute only on the 'post' content type
    global $post_type;
    if($post_type == 'post'){

        $user_role="";
        // Get all user roles
        $user_roles = array();
        foreach ( get_editable_roles() as $key => $values ) :
            $user_roles[ $key ] = $values['name'];
        endforeach;
        // Set a selected user role
        if ( ! empty( $_GET['user_role'] ) ) {
            $user_role  = sanitize_text_field( $_GET['user_role'] );
        }

        ?><select name="user_role">
        <option value=""><?php _e( 'All Roles', 'papasemarone' ); ?></option><?php
        foreach ( $user_roles as $key => $value ) :
            ?><option <?php selected( $user_role, $key ); ?> value="<?php echo $key; ?>"><?php echo $value; ?></option><?php
        endforeach;
        ?></select><?php

    }

}
add_action('restrict_manage_posts','add_role_filter_to_posts_administration');

function add_role_filter_to_posts_query( $query ) {

    /**
     * No use on front
     * pre get posts runs everywhere
     * even if you test $pagenow after, bail as soon as possible
     */
    if ( ! is_admin() ) {
        return;
    }

    global $pagenow;

    /**
     * use $query parameter instead of global $post_type
     */
    if ( 'edit.php' === $pagenow && 'post' === $query->query['post_type'] ) {

        if ( isset( $_GET['user_role'] ) ) {
            $role = $_GET['user_role']; // return string 'contributor_facebook'
            $users   = new WP_User_Query( array( 'role' => $role ) );
            $results = $users->get_results();

            $user_ids = array();
            foreach( $results as $result ) {
                $user_ids[] = (int) $result->ID;
            }

            /**
             * I use PHP_INT_MAX here cause 0 would not work
             * this means "user that does not exist"
             * this trick will make WP_Query return 0 posts
             */
            $user_ids = ! empty( $user_ids ) ? $user_ids : PHP_INT_MAX;

            $query->set( 'author__in', $user_ids );

        }

    }

}
add_action( 'pre_get_posts', 'add_role_filter_to_posts_query' );

You need to make a User query to get all users that have specific role and then query posts with the “author” parameter.

Leave a Comment