1
class MyPlugin {

    public function __construct() {
        add_action('init', array(&$this, 'do_member_filter'));
    }

    public function do_member_filter() {
        $users = new WP_User_Query( array(
            'meta_query' => array(
                array(
                    'key'     => "{$GLOBALS['wpdb']->prefix}capabilities",
                    'value'   => 'abcrole',
                    'compare' => '!='
                )
            )
        ));


        do_action_ref_array('pre_user_query', $users);
    }
}
1

How to use the class methods in the callback

The $query object inside pre_user_query is a fully qualified core object, so you can use $query->set( 'key', 'value' ); as well as $query->get( 'key' );.

If you got the problem that this might interfere with other callbacks, then simply add remove_filter( current_filter(), __FUNCTION__ ); to your callback, so it removes itself during the first call.

Another way to go

You can as well use those arguments directly when instantiating the class:

$users = new WP_User_Query( array(
    'meta_query' => array(
        'relation'    => 'OR',
        array(
            'key'     => "{$GLOBALS['wpdb']->prefix}capabilities",
            'value'   => 'abcrole',
            'compare' => '!='
        ),
        array(
            'key'     => "{$GLOBALS['wpdb']->prefix}capabilities",
            'value'   => 'ab',
            'compare' => 'NOT LIKE'
        )
    )
) );

Benefits

This example shows how you can exclude users by a capability or role that matches exactly or is only named LIKE the role you'd like to exclude. This comes handy when you for example prefix some of your roles or capabilities and then want to mass-exclude them.

You can as well just use the same in the callback. Pay attention that you use an array( array() ) for your meta query.

  • thank you! I tried both solution but it doesn't work for me. The first sulotion which is the $query->set() doesn't work as expected and then the second one resulted to a "Problem loading page". Infinite loop maybe going on on the second solution I guess. I am firing this one on the pre_user_query action. – Godwinh Jun 6 '13 at 9:37
  • @Godwinh Then please update your question and show us all of your surrounding code. – kaiser Jun 6 '13 at 9:38
  • It is now updated with 1st and 2nd solution – Godwinh Jun 6 '13 at 9:45
  • I have edited my code above again but still doesn't work. I have checked if there are other callbacks attached to the hook but there isn't any. – Godwinh Jun 7 '13 at 6:34
  • Please do a var_dump( $GLOBALS['wp_query'] ); and show the meta_query part after you used the code from my answer. You could as well dump inside the posts_clauses filter and show us the dump. – kaiser Jun 7 '13 at 9:37

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.