Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save champsupertramp/cb43f0d079b9160cd13238c75dd76257 to your computer and use it in GitHub Desktop.
Save champsupertramp/cb43f0d079b9160cd13238c75dd76257 to your computer and use it in GitHub Desktop.
Ultimate Member - UM Custom user query in Member Directory
<?php
/**
* Show users with a Job Title "'WP Plugin developer " only
**/
add_filter('um_prepare_user_query_args', 'um_my_custom_query_args', 99, 2);
function um_my_custom_query_args( $query_args, $args ) {
if( $args["form_id"] == "1" ) { // you can validate the current member directory form ID
$query_args['meta_query'][] = array(
"relation" => "OR",
array(
'key' => 'job_title',
'value' => serialize( 'WP Plugin developer' ),
'compare' => 'LIKE'
),
array(
'key' => 'job_title',
'value' => 'WP Plugin developer',
'compare' => '='
)
);
} // endif
return $query_args;
}
?>
@kondorwithak
Copy link

Wow! Another fantastic piece of code for Ultimate Member. I searched all over for the directory search arguments and the um_prepapre_user_query_args was exactly the spot. Nothing but praise for you and your top-notch code tonight!

@jabumeri
Copy link

where do you put this piece of code in UM? Thanks!

@h-squared
Copy link

Great piece of code!
I'm trying to work out a variant of this but not succeeding. I need the option to show users with different 'Job titles' as well depending on another condition. Could you please help? Thank you.

@tplc2016
Copy link

tplc2016 commented Oct 14, 2017

I am currently using a member directory that has 12 different fields that can be used as filters to come up with a search result. The filters each correspond to a custom field of the UM member's profile. Unfortunately it seems that MySQL can't take such a huge query and the search results often time out. Does your code allow us to limit search results to certain meta key values before the search actually happens? If so, that would put less demand on the MySQL query and that would help us a lot with performance issues.

@mattkeenan
Copy link

I have had a similar issue, and I found that the user query we were using was killed by two things. Firstly when querying for the users the query as passed to MySQL used SELECT DISTINCT SQL_CALC_FOUND_ROWS as well as ORDER BY RAND(). Both of these kill performance forcing MySQL to use temporary and filesort, if you are running on a server with HDD then your performance will die. As little as 5,000 users with several roles can mean a user look up can take nearly 60 seconds. Loading everything into foo_usermeta just kills performance; a pity that WP doesn't support PostgresQL, hstore is a godsend for that sort of thing.

@champsupertramp
Copy link
Author

Hi Everyone,

I'll be sharing some Ultimate Member customizations & tutorials on my website: www.champ.ninja

Subscribe to my newsletter to get the latest tutorials.

Regards,

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment