Skip to content

Instantly share code, notes, and snippets.

@helgatheviking
Created July 7, 2013 15:41
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save helgatheviking/5943857 to your computer and use it in GitHub Desktop.
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"
<?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');
<?php
/**
* The Template for displaying Author Search
*
* Place this file in your theme
* yourtheme/simple-user-listing/search-author.php
*
*/
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
$search = ( get_query_var( 'billing_city' ) ) ? get_query_var( 'billing_city' ) : '';
global $sul_users;
?>
<div class="author-search">
<h2><?php _e('Search authors by city' ,'simple-user-listing');?></h2>
<form method="get" id="sul-searchform" action="<?php the_permalink() ?>">
<label for="as" class="assistive-text"><?php _e('Search' ,'simple-user-listing');?></label>
<input type="text" class="field" name="billing_city" id="sul-s" placeholder="<?php _e('Search Authors' ,'simple-user-listing');?>" value="<?php echo $search; ?>"/>
<input type="submit" class="submit" id="sul-searchsubmit" value="<?php _e('Search Authors' ,'simple-user-listing');?>" />
</form>
<?php
if( $search ){ ?>
<h2 ><?php printf( __('Search Results for: %s' ,'simple-user-listing'), '<em>' . $search .'</em>' );?></h2>
<a href="<?php the_permalink(); ?>"><?php _e('Back To Author Listing' ,'simple-user-listing');?></a>
<?php } ?>
</div><!-- .author-search -->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment