WP_user_query throws a 404 error

I’m trying to get WordPress (3.9.2) to search meta_key’s in usermeta.

I have tried every example provided on every site I’ve searched but I keep getting redirected to the 404 error page.

I’ve tried the simplest check yet and it still throws an error – can someone please help me understand what I’m doing wrong?

This works:

add_action('pre_user_query','my_pre_user_search');
function my_pre_user_search($user_query) {
   print_r($user_query);
}

Which outputs this:

WP_User_Query Object
(
    [query_vars] => Array
        (
            [blog_id] => 1
            [role] => 
            [meta_key] => 
            [meta_value] => 
            [meta_compare] => 
            [include] => Array
                (
                )

            [exclude] => Array
                (
                )

            [search] => 
            [search_columns] => Array
                (
                )

            [orderby] => login
            [order] => ASC
            [offset] => 0
            [number] => 20
            [count_total] => 1
            [fields] => all_with_meta
            [who] => 
        )

    [results] => 
    [total_users] => 0
    [query_fields] => SQL_CALC_FOUND_ROWS wp_users.ID
    [query_from] => FROM wp_users
    [query_where] => WHERE 1=1
    [query_orderby] => ORDER BY user_login ASC
    [query_limit] => LIMIT 20
)

However, when I try to modify the query to: (taken exactly from http://codex.wordpress.org/Class_Reference/WP_User_Query)

add_action('pre_user_query','my_pre_user_search');
function my_pre_user_search($user_query) {
   $user_query = new WP_User_Query( array( 'meta_key' => 'country', 'meta_value' => 'Israel' ) );
}

The page redirects to a 404 Error Page

As does this: (which is modified to search a valid metakey and value

add_action('pre_user_query','my_pre_user_search');
function my_pre_user_search($user_query) {
   $user_query = new WP_User_Query( array( 'meta_key' => 'member_id', 'meta_value' => '2349' ) );
}

I’m copying the code character for character from WP but it is not working…

Any ideas?

Solutions Collecting From Web of "WP_user_query throws a 404 error"

pre_user_query is called after the query variable object is created, but before the actual query is run. Your code is creating a new query within the action when you should actually modify the existing query’s arguments using the $query->set() method. For example:

function my_pre_user_search($user_query) {
  $meta_query = array(array(
    'key' => 'member_id',
    'value' => '2349'
  ));
  $user_query->set('meta_query', $meta_query);
}
add_action('pre_user_query','my_pre_user_search');