admin管理员组

文章数量:1425735

I'm tyrying to let the admin search through the users by a single meta value called 'indice de busqueda' insithe the wordpress /wp-admin/users.php page

How can I hook into the search box in such a way so that I can change the query to look something like this?

    $query = get_users([
      'meta_key' => 'indice de busqueda',
      'meta_value' => $search_value,
      'meta_compare'=>'REGEX'
    ])

I tried many possible aproaches but I cant seem to find one where I can avoid messing with SQL.

I'm tyrying to let the admin search through the users by a single meta value called 'indice de busqueda' insithe the wordpress /wp-admin/users.php page

How can I hook into the search box in such a way so that I can change the query to look something like this?

    $query = get_users([
      'meta_key' => 'indice de busqueda',
      'meta_value' => $search_value,
      'meta_compare'=>'REGEX'
    ])

I tried many possible aproaches but I cant seem to find one where I can avoid messing with SQL.

Share Improve this question asked May 30, 2019 at 21:14 Joaquin BrandanJoaquin Brandan 1033 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 1

Take a look at the pre_get_users hook. It is very similar to pre_get_posts if you are familiar with that, except it is for wp_user_query instead of wp_query.

You will need to ensure that you only check the users screen in the dashboard, because that hook would affect any user listing otherwise.

Here is an example of what the code might look like. Not fully tested but based on some working code from my own plugin.

function so339209_filter_users_indice_de_busqueda( $query ) {
    if ( !function_exists('get_current_screen') ) return;

    // Only apply on the Users screen in the dashboard
    $screen = get_current_screen();
    if ( !screen || screen->in !== 'users' ) return;

    $query->set('meta_key', 'indice de busqueda');
    $query->set('meta_value', $search_value);
    $query->set('meta_compare', 'REGEX');
}
add_action( 'pre_get_users', 'so339209_filter_users_indice_de_busqueda', 20 );

I found the problem.

The query from pre_get_users was returning empty because wordpress was appending and prepending asterisks * to my search string. if i searched for 11 then $query->query_vars['search'] would be equal to '*11*' instead of 11 and that was screwing up all of my searches, regex or otherwise.

The solution was to remove the asterisks from the search value

function so339209_filter_users_indice_de_busqueda($query)
{
    global $pagenow;

    if (is_admin() && 'users.php' == $pagenow) {


        //Remove trailing and starting empty spaces
        $the_search = trim($query->query_vars['search']);

        //Remove trailing and starting asterisks that wordpress adds to your search
        // string
        $the_search = trim($query->query_vars['search'], '*');

        //Build your query from the search string, Im using a LIKE comparison 
        //for simplicity but you could use a REGEX too
        $query->set('meta_key', 'indice de busqueda');
        $query->set('meta_value', $the_search);
        $query->set('meta_compare', 'LIKE');

        //Nuke the search string so that the query does not try to match your search
        //with the users username or email.
        $query->set('search', '');
    }
}
add_action('pre_get_users', 'so339209_filter_users_indice_de_busqueda', 20);

本文标签: