Skip to content

Instantly share code, notes, and snippets.

@5t3ph
Last active August 29, 2015 14:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 5t3ph/f87dad1fac5f6d920c1a to your computer and use it in GitHub Desktop.
Save 5t3ph/f87dad1fac5f6d920c1a to your computer and use it in GitHub Desktop.
WordPress WP_User_Query Transient Array Search
<?php
/*
* WordPress WP_User_Query Transient Array Search
*
* Purpose: To search array of relevant user fields
* stored as an array in a transient
*
* Example Array:
* [userid] => array (
'0' => 'first_name',
'1' => 'last_name',
'2' => 'custom_id'
)
*
* @param array $array Input array to search for values
* @param array $search Passed array is created prior to function by an explode(' ', $user_entered_term)
*
* @return array userid for each match
*/
function sd_student_search($array, $search) {
// Est. array with dummy value (better way??)
// Otherwise WP_User_Query will include everyone instead of no one when no values found
$found = array('prevent_show_all');
// Define $search as array if input as string
if(!is_array($search))
$search = array($search);
foreach($search as $term) {
$term = strtolower($term);
foreach($array as $key => $values) {
foreach($values as $index => $data) {
$data = strtolower($data);
if( strpos($data, $term) !== FALSE ) {
$found[] = $key;
}
}
}
}
return $found;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment