You need to create another search form, get the text value and then you can fire your query.
Place below code where you want to display form.
<form role="search" method="get" id="searchform" class="searchform" action="<php echo site_url('/'); ?>">
<div class="user-form">
<label class="screen-reader-text" for="s">Search for:</label>
<input type="text" value="<?php echo get_search_query(); ?>" name="s" id="s" />
<input type="submit" id="searchsubmit" value="Search" />
</div>
</form>
Place this (below code) where you need to need to display in your template file.
if( $_GET['s'] ) {
$search_term = $_GET['s'];
----
Execute your code here
----
}
You can change for name, id as per your wish for better understanding.
Hi! Thank you for this code that makes a lot of sense. So I did as you said.
The form is displayed and and the query is in a template page named user-search.php linked to the page user-search in wp. But still I can not get the query to work. The page is not found. It seems I am having trouble to connect both form and query code.
What am I missing?
I am getting the following in url bar for the existing word linux for instance after hitting the search button /<php%20echo%20site_url(/);?s=linux and the page is not found.
Does the query should work on the page itself (without the form) if I use the following
/user-search/?s=linux ? Cos im getting page is not found again here.
TY
I hope Anurag doesn’t mind my jumping in, in an effort to get a quicker response back to you. There is a typo in Anurag’s form tag. Use this instead:
// added missing ? to "<php" and added user-search to path
<form role="search" method="get" id="searchform" class="searchform" action="<?php echo site_url('/user-search/'); ?>">
You also should sanitize the passed search term before using it in a query, otherwise you could be subject to SQL injection attacks:
if( $_GET['s'] ) {
$search_term = sanitize_text_field( stripslashes( $_GET['s']));
----
Execute your code here
----
} else {
echo 'Please go back and provide a search term.';
}
The else condition I added isn’t required, but it improves the user experience a little. I also added stripslashes() because some names have apostrophes (e.g. “O’Brien”), which are escaped when added to $_GET. stripslashes() unescapes them.
Hi BC,
So glad you jumped in there. I obviously didn’t see the missing ‘?’ Thank you also for the SQL warning I will do the sanitize from now on.
Well now I can say the form is working but still I am now facing a weird behaviour on the result page :/
In fact only few single letters can give me the page displaying some user …
for instance /user-search/?s=e but a complete existing word does not and I don’t even see see the else condition (no author found) ….
Here the code
if( $_GET['s'] ) {
$search_term = sanitize_text_field( stripslashes( $_GET['s']));
// WP_User_Query arguments
$args = array (
'role' => 'author',
'order' => 'ASC',
'orderby' => 'display_name',
'search' => '*' . esc_attr( $search_term ) . '*',
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'first_name',
'value' => $search_term,
'compare' => 'LIKE'
),
array(
'key' => 'last_name',
'value' => $search_term,
'compare' => 'LIKE'
),
array(
'key' => 'description',
'value' => $search_term,
'compare' => 'LIKE'
)
)
);
// Create the WP_User_Query object
$wp_user_query = new WP_User_Query( $args );
// Get the results
$authors = $wp_user_query->get_results();
// Check for results
if ( ! empty( $authors ) ) {
echo '<ul>';
// loop through each author
foreach ( $authors as $author ) {
// get all the user's data
$author_info = get_userdata( $author->ID );
echo '<li>' . $author_info->nickname . ' ' . $author_info->last_name . '</li>';
echo '<li>' . $author_info->description . '</li><br>';
}
echo '</ul>';
}
else {
echo 'No authors found';
}
}
Thank you so much
Thanks @bcworkz for rectifying that.
Hello everyone,
Got it to work finally.
I got rid of 'search' => '*' . esc_attr( $search_term ) . '*',
and changed the ‘s’ parameter to something else.
1Million thanks to you guys for helping me out.
-
This reply was modified 7 years, 10 months ago by
futur3int.
Happy to help (to both of you)
FYI : * is not a wildcard character in SQL, % is. In any case when using meta query’s LIKE, WP adds wildcards front and back. Adding your own causes WP to treat your wildcard as a string literal that must be matched in any meta value, so the query is unlikely to find any matches.