Created
July 7, 2013 15:41
-
-
Save helgatheviking/5943857 to your computer and use it in GitHub Desktop.
Example of a working WP_User_Query search by simple user meta using Simple User Listing
In this example I am searching for users by the meta field "billing_city"
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Place this in your theme's functions.php file | |
* Or a site-specific plugin | |
* | |
*/ | |
// Switch the WP_User_Query args to a meta search | |
function kia_meta_search( $args ){ | |
// this $_GET is the name field of the custom input in search-author.php | |
$search = ( isset($_GET['billing_city']) ) ? sanitize_text_field($_GET['billing_city']) : false ; | |
if ( $search ){ | |
// if your shortcode has a 'role' parameter defined it will be maintained | |
// unless you choose to unset the role parameter by uncommenting the following | |
// unset( $args['role'] ); | |
$args['meta_key'] = 'billing_city'; | |
$args['meta_value'] = $search; | |
$args['meta_compare'] = '='; | |
} | |
return $args; | |
} | |
add_filter('sul_user_query_args', 'kia_meta_search'); | |
// Register query var and whitelist with Simple User Listing | |
function kia_search_vars( $vars ){ | |
$vars[] = 'billing_city'; | |
return $vars; | |
} | |
add_filter('sul_user_allowed_search_vars', 'kia_search_vars'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment