wp admin - making users.php search include a specific user meta data field without messing with the SQL query itself

I'm tyrying to let the adminsearch through the users by a single meta value called 'indice de busqueda'

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745447221a4628102.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信