It took a LOT longer than it should have to build an efficient query for WordPress users today. The query — an WP_User_Query INVERSE search.

The list I was trying to generate was based on two things:
– All users with a meta_key field ‘account_status’ containing the status ‘active’
– EXCLUDE all users with a nicename ending with “at_slp_dot_guru”

Turns out this is a LOT more difficult than it should be due to shortcomings in the WP_User_Query class. The solution that created the least amount of data queries , and thus improved performance over other “post filter” methods was to make use of the pre_user_query action hook that is part of WP_User_Query.

The Patch

The short version of the patch — use the pre_user_query to change an “INCLUDE all users with this search value” to an EXCLUDE for those same users. This is done by using the INCLUDE search filter than “flipping the logic” by replacing the field comparison in the WordPress-generated query to NOT LIKE instead of LIKE.

The implementation

To use this modification I build my query with intentional reversal of the primary data fields (user login, user name, user nicename, etc. are all primary WP_User_Query fields). In my case, search for all users that INCLUDE the ‘at_slp_dot_guru’ nicename.

I setup the WordPress action filter for ‘pre_user_query’, which takes the full WP_User_Query as a passed-by-reference object, to flip the logic from INCLUDE to EXCLUDE as noted above.


The above code is an over-simplified version of the filter. Since WordPress builds the search query to include ALL fields, not just the one specified in ‘search_fields’ you will need to use a regular expression to replace all of the LIKE phrases with NOT LIKE and OR with AND in the search match expression.

A huge pain — in my implementation I ended up doing this as a filter:

End Result

When the get_results() query is executed I can now see “all active accounts EXCEPT those that end with _at_slp_dot_guru”.

WordPress Feedback

Some possible ways that WordPress could extend WP_User_Query to accommodate complex inverse logic without a ton of overhead —

Add a ‘search_compare’ setting that works exactly the the META field operators: ‘!=’, ‘NOT LIKE’ along with the current search option which is hard-coded to ‘LIKE’. It is odd that primary fields do not have the same power as the search engine for meta fields. Not too mention the lack of consistency in the way the two query components are defined.

Allow user queries to process both an INCLUDE list and EXCLUDE list. Currently ‘include’ takes precedence when the parameter is used along with ‘exclude’ — intead of using if/else-if use back-to-back if statements allowing both filters to be used at the same time:

Update to this:

When search_fields is specified in a search filter
match in WP_User_Query do NOT then build an SQL where clause that adds ALL the user fields including user_login, displayname, and all the others. Only add the specified search_field to the query.

Share Your Insight

%d bloggers like this: