-
-
Save tommcfarlin/19f37fe583f4bc0b1762 to your computer and use it in GitHub Desktop.
[WordPress] An example for how to use WP_Query to search for a person by their first name or their last name.
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 | |
/* 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