Last active
August 29, 2015 14:08
-
-
Save 5t3ph/f87dad1fac5f6d920c1a to your computer and use it in GitHub Desktop.
WordPress WP_User_Query Transient Array Search
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 | |
/* | |
* 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