Skip to content

Instantly share code, notes, and snippets.

@tommcfarlin tommcfarlin/1-name-search.php Secret
Created Jan 20, 2015

Embed
What would you like to do?
[WordPress] An example for how to use WP_Query to search for a person by their first name or their last name.
<?php
/* First, verify that the hidden field contains the 'true' value.
*
* Note that this field could be controlled by JavaScript such that
* if the end user opted to search by something else, then this field
* could have a different value and branch to a different conditional.
*/
if ( 'true' == $_POST['member-search-field-searching'] ) {
/* Read the meta data from the provided fields. The meta key
* is the first name or last name, the value is the content
* of the text input field.
*/
$meta_key = $_POST['member-search-type'];
$meta_value = $_POST['member-search-field'];
// Create an array to store the results of the search
$users = array();
// Look for all administrators with the specified name
$args = array(
'fields' => 'all_with_meta',
'role' => 'adminsitrator',
'meta_key' => $meta_key,
'meta_value' => $meta_value
);
$user_query = new WP_User_Query($args);
// Store the results in the array that was created.
$users = $user_query->get_results();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.