• Hello,
    I am using this code to which I have added “All Authors”.
    But I can’t in any way add an entry that cancels the user filter, so that it no longer filters only the roles listed below.

    
    add_action('restrict_manage_posts', 'ditt_filter_by_author');
    function ditt_filter_by_author() {
            $params = array(
                'name' => 'author',
    			'show_option_all' => 'All Authors',
                'role__in' => array('author','editor')
            );
            if ( isset($_GET['user']) ) {
                $params['selected'] = $_GET['user'];
        }
        wp_dropdown_users( $params );
    }

    Do you have feedback to do this?
    Thank you

    • This topic was modified 1 year, 10 months ago by woodypad.
Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator bcworkz

    (@bcworkz)

    I’m unclear on what filter you want cancelled. Filtering the posts query itself or the user query to possibly filter posts by? “All Authors” should effectively cancel the posts query filtering by author, doesn’t it? So can I assume you want the drop down to not filter users and list all of them? But that doesn’t make sense either. Not all users can author posts. I suggest using the “capability” arg instead of “role__in”. (replaces the “who” arg that has been deprecated since 5.9)

    Replace the “role__in” arg with
    'capability' => 'edit_posts',
    This will list all users who could possibly author posts. This will not eliminate users who could author a post but have not done so. If you want only users who have actually authored posts, I think you’d need to query for all posts, getting only distinct post_author field values. Use the result as the “include” arg value.

    Thread Starter woodypad

    (@woodypad)

    Hi,
    currently my filter works well, it allows me to filter articles by author name, with respect to the roles I have entered.
    The only problem is that if I select “All author” it always searches among the authors / roles included in this filter.
    I would like to create a voice that seeks out all possible writers.

    For example: the site administrator has written articles, I deliberately did not mention the roles, but if I carry out a generic search using the filters selecting all, it does not find the contents of other unmentioned roles, in this case administrator.

    Another example: this filter is also visualized inside some plugins that use custom post type.
    I am using an ad plugin, only admin roles can use it, but if I try to use filters, they are all bound to the roles contained in the filter and therefore does not find the ads I am trying to filter.

    Thank you

    Moderator bcworkz

    (@bcworkz)

    The role__in arg should only affect the query for user names for the dropdown, it doesn’t affect the posts query. The All Authors option yields a author=0 query var, which should result in posts by any author being listed regardless of author role. A posts query doesn’t even have an option to qualify results by author role. Something else is restricting your posts query.

    Regardless of what query vars are passed, you can alter the actual query through the “pre_get_posts” action. There you could unset whatever undesirable query vars that had been set. To avoid altering the wrong query, verify you are working on the main query and that is_admin() returns true. Add your action callback with a large $priority arg to help ensure your query modification is done after any others.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Filter Author null’ is closed to new replies.