1

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?

  • 1
    Watch out for recursive loops! – birgire Aug 13 '14 at 3:23
  • Why are you hooking into pre_user_query ? If you need results, this - $user_query = new WP_User_Query( array( 'meta_key' => 'member_id', 'meta_value' => '2349' ) ); should return the results you need. – Shazzad Aug 13 '14 at 4:03
1

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');

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.