4

Initially I thought I'd use get_users() but it turned out it doesn't support the display_name field. Then I've read about using WP_User_Query, but when I do it always exhausts the allowed memory size of 134217728 bytes. When I remove the memory limit this part of the page doesn't even try to load. Now I'm thinking about running a function that adds everyone's display_name to their user meta so I can use get_users() again. Is that a good idea?

My objective is to make the search also sort by relevance and I plan to do it by using explode(' ', $search_query) then call get_users() for each element in the array, then combine and sort the results by occurance like it's shown here: https://stackoverflow.com/questions/2176626/php-get-element-with-5-highest-occurrence-in-an-array and I'm not sure if that's going to be memory efficient.

Thanks for reading.

2

I think you should use the default class for this job - WP_User_Query. The query have a lot of possibilities, also get the display name. WP_User_Query is a class that allows querying WordPress database tables _users and _usermeta.

Also it is helpful when you use a cache for the values. Also here I prefer the WordPress defaults WP_Cache (Non-Persistent Cache) or transient API (Database-Driven Temporarily Persistent Cache).

The example below will demonstrate this with WP_Cache, you can also doing this with transients. The code will not work, is only write by scratch to illustrate.

function wpse_get_user_data( $args ) {


    if ( ! $user_query = wp_cache_get( $args->ID, 'your_key' ) ) {
        $user_query = array();
        $user_query = new WP_User_Query( $args );

        wp_cache_add( $args-ID, $user_query, 'your_key' );
    }

    return $user_query;
}
  • Thanks for your reply, but seems like the memory still needs to be used before adding anything to the cache. The moment the line containing WP_User_Query starts executing I get a fatal error about exhausting the allowed memory size. – Nadroev May 20 '15 at 13:31
  • How much users do you have? But you can reduce the default query, that you use only the query about one field. – bueltge May 20 '15 at 17:26
  • 1700, is that much? Wouldn't be good if one can't search through all users, but just for testing, how can I do the thing you said? – Nadroev May 21 '15 at 5:27
  • No, is much for a lot of installs. But we have a lot of clients, there have much more. The second answer demonstrate the reduction of a smaller search, faster, lower memory. – bueltge May 21 '15 at 11:06
  • Sorry, could you rephrase that? I don't think I understood it at all. – Nadroev May 25 '15 at 11:31
1

If you push a "search" through WP_User_query you will search both the user_login fields and the user_nicename fields. That produces a de facto inefficient OR query. You can reduce that by using a filter to force the search to only the user_nicename field.

add_filter('user_search_columns','nicename_only_wpse_188846');

function nicename_only_wpse_188846($search_columns) {
  return array('user_nicename');
}

$args = array(
  'search' => 'abce',
);

// The Query
$user_query = new WP_User_Query( $args );
var_dump($user_query);

Combine that with caching and you might have a shot.

That said, 134217728 bytes is pretty slim if you have a lot of users and traffic. You may just need a server upgrade.

  • Currently I am still testing that locally, so that's just the limit set in the ini file. Also the page doesn't load not only when I remove the limit, but also when I set the limit to be enough for the current case. I know I have enough space on my C drive. – Nadroev May 20 '15 at 15:35

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.