Dolunaykiz
Dolunaykiz

Reputation: 333

Blog Post Author Filter in WordPress

I want to put an Author filter dropdown into the WP Admin backend. As in, not just filter by category or tag, but also by author.

The following code works for the most part, but it gives me ALL the registered users, including those who don't have author capability. I want to filter this list to only show me non-subscribers, but, being a newbie at PHP, I can't seem to figure it out. Either I break my site, or the code inserted does absolutely nothing.

    /**
    * This section makes posts in the admin filterable by the author.
    */
    add_action('restrict_manage_posts', 'ditt_filter_by_author');
    function ditt_filter_by_author() {
            $params = array(
                'name' => 'author',
                'show_option_all' => 'All Authors' 
            );
            if ( isset($_GET['user']) ) {
                $params['selected'] = $_GET['user'];
        }
        wp_dropdown_users( $params );
    }

Any pointers are appreciated.

Upvotes: 0

Views: 936

Answers (1)

DaiYoukai
DaiYoukai

Reputation: 1898

Use role__in or role to limit it to specific user roles.

 /**
* This section makes posts in the admin filterable by the author.
*/
add_action('restrict_manage_posts', 'ditt_filter_by_author');
function ditt_filter_by_author() {
        $params = array(
            'name' => 'author',
            'role__in' => array('author','editor','administrator')
        );
        if ( isset($_GET['user']) ) {
            $params['selected'] = $_GET['user'];
    }
    wp_dropdown_users( $params );
}

This will filter it to show only authors, editors, and administrators.

Upvotes: 1

Related Questions