Support

Account

Home Forums Front-end Issues User field – Select2 doesn't search for names

Solved

User field – Select2 doesn't search for names

  • Hello!
    In some previous version it was OK but now I have an issue.
    I have User field in my form which returns a Select2 with search feature.
    For example I have user with login “Ivan” and name “Test”
    In select I see “Ivan (Test)” and I can find him typing “Ivan”. But when I enter “Test” – nothing found.
    I need to find users by their names.
    Can I fix it?
    Thanks.

  • I was going to tell you there were some new filters, but then I went and read this: https://codex.wordpress.org/Function_Reference/get_users

    Under search it says:

    search – Use this argument to search users by email address, URL, ID or username (this does not currently include display name).

    This is something that is lacking in WP and searching by display name is not currently available. This is something that needs to be taken up with the WP team. There may already be something about this on trac https://core.trac.wordpress.org/

  • Thanks for your answer.
    But, as I understand correctly, user search in ACF is provided by Select2 and not by get_users()
    I’ve recorded video of this issue.
    Search by name is not working in 5.2.7 version, but it does in 5.2.1:
    https://youtu.be/G1jrSyOe-5s

  • Select2 is just a JavaScript api that fills the fields and allows dong so using AJAX, it does not connect directly to the DB to get information or alter the way WP works. The underlying function that gets those values for display is still part of WP.

    As you can see by looking at the php file for the users field (/fields/user.php) on lines 129 to 135.

    
    // filters
    $args = apply_filters("acf/fields/user/query", $args, $field, $options['post_id']);
    $args = apply_filters("acf/fields/user/query/name={$field['_name']}", $args, $field, $options['post_id']);
    $args = apply_filters("acf/fields/user/query/key={$field['key']}", $args, $field, $options['post_id']);
    
    // get users
    $users = get_users( $args );
    
  • OK, but why it works on 5.2.1 and doesn’t work on 5.2.7? As you can see on video

  • I can’t say. I will flag this discussion for the developer. I was not aware that it worked in a previous version.

  • OK, thanks.
    I’ll be using old version till fix.

    • Elliot

    • July 8, 2015 at 10:23 am

    Hi @ruslancc

    ACF PRO 5.2.1 oiverrides the search columns to:
    array('user_login', 'display_name')

    ACF PRO 5.2.7 does not – it lets WP decide which columns to search. By default these columns should be the same, but it is possible that something is modifying them.

    I added in a filter to allow customization to the search columns, You can see if being used here:

    
    // filter for 3rd party customization
    		$columns = apply_filters("acf/fields/user/search_columns", 							$columns, $search, $WP_User_Query, $field);
    		$columns = apply_filters("acf/fields/user/search_columns/name={$field['_name']}",	$columns, $search, $WP_User_Query, $field);
    		$columns = apply_filters("acf/fields/user/search_columns/key={$field['key']}",		$columns, $search, $WP_User_Query, $field);
    

    So you can hook into the acf/fields/user/search_columns filter and modify the $columns to what you want.

    Please also debug the $columns and see what they are set to. Post back the result and I’ll see if I need to manually add back in the ‘display_name’.

    Thanks
    E

  • Hi Elliot!
    Thanks for the greatest plugin for WP and for your answer 🙂
    var_dump() says that my $columns is NULL.

    But I fixed my problem by this code in my functions.php:

    add_action( 'user_search_columns', 'acf_name_search' );
    function acf_name_search() {
        $columns[] = "display_name";
    	return $columns;
    }

    Now it works great for me.

  • I think it bothers me more that the codex is wrong than that I didn’t know ACF actually did it and now does not. If I was looking for this that’s were I would have turned. I have not had much need for sorting users as yet. Thanks E, I’m not likely to forget this bit of info.

  • The solution is change the function get_result on class-acf-field-user.php

    Change $result = $user->user_login; to $result = $user->$user->first_name or appe d first and lastname:

    $result .= $user->first_name;
    if( $user->last_name ) {$result .= ‘ ‘ . $user->last_name;};

Viewing 12 posts - 1 through 12 (of 12 total)

You must be logged in to reply to this topic.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.