3

how can i filter by roles in wordpress admin

im already add filter by roles dropdown enter image description here

using this code http://pastebin.com/Zbv3UhVH

but im stuck on pre_get_posts

function add_role_filter_to_posts_query( $query ) {

    global $post_type, $pagenow;

    if ($pagenow == 'edit.php' && $post_type == 'post') {

        if ( isset( $_GET['user_role'] ) ) {

            $role = $_GET['user_role'] ); // return string 'contributor_facebook'

            // TO DO 

        }

    }

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

what i must to do, to filter it..

thanks

3

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.

4
  • Don't you need author__in instead of the author argument ? What about the simplification: get_users( ['role' => $role, 'fields' => 'ID' ] ) ?
    – birgire
    Aug 15 '16 at 11:56
  • 1
    I believe in queries ^^ and author parameter is fine, it takes author IDs as a string but author__in is a better approach, you're right, no need for implode, I've edited my answer. Thanks :)
    – JMau
    Aug 15 '16 at 20:03
  • yes, I forgot about the csv support of the author attribute ;-) I think it's possible to simplify further by using the fields parameter to return only the user ID.
    – birgire
    Aug 15 '16 at 20:27
  • @birgire yes that's true but I prefer WP_User_Query interface, performance is fine, plus we are in admin area.
    – JMau
    Aug 16 '16 at 19:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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