can't search by email wordpress - wordpress

I create a search form with auto-completion. Now my search works by first and last name. But not by mail. What's wrong?
$args = array (
'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' => 'user_email',
'value' => $search_term,
'compare' => 'LIKE'
)
)
);
// Create the WP_User_Query object
$wp_user_query = new WP_User_Query( $args );

User e-mail is not meta, so you can't use meta_query. You can use separeted array key search_columns. If you want results, where $search_term contains only one of the options for first_name, last_name or user_email, I found a functional way when I merge two queries (query for search by user_email and your original adapted query). This works for me:
$args1 = array (
'order' => 'ASC',
'orderby' => 'display_name',
'search' => '*' . esc_attr( $search_term ) . '*',
'search_columns' => array( 'user_email' )
);
$args2 = array (
'order' => 'ASC',
'orderby' => 'display_name',
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'first_name',
'value' => $search_term,
'compare' => 'LIKE'
),
array(
'key' => 'last_name',
'value' => $search_term,
'compare' => 'LIKE'
)
)
);
$query1 = new WP_User_Query( $args1 );
$query2 = new WP_User_Query( $args2 );
$wp_user_query = new WP_User_Query();
$wp_user_query->results = array_merge( $query1->results,$query2->results );

Try this approach
$args = array(
'order' => 'ASC',
'orderby' => 'display_name',
'search' => '*' . esc_attr( $search_term ) . '*',
'search_columns' => array( 'first_name', 'last_name', 'user_email' )
);
$user_query = new WP_User_Query( $args );
More info about WP_User_Query class here https://codex.wordpress.org/Class_Reference/WP_User_Query
EDIT:
Available search fields
'ID' - Search by user id.
'user_login' - Search by user login.
'user_nicename' - Search by user nicename.
'user_email' - Search by user email.
'user_url' - Search by user url.

Related

Wordpress: search users using 'first_name' and 'last_name'?

UPDATE: I mean to search user in the Admin User Dashboard at /wp-admin/users.php
Is there a way to search users using 'first_name' and 'last_name'?
I found this plugin but not working at all: https://wordpress.org/plugins/full-name-search-in-wp-admin/
I also found this function, but neither is working in my site. Do I need to call the function with a hook/action?:
function search_users($search_term){
global $wpdb;
$the_users = $wpdb->get_results("SELECT DISTINCT $wpdb->users.* FROM $wpdb->users INNER JOIN $wpdb->usermeta um1 ON um1.user_id = $wpdb->users.ID JOIN $wpdb->usermeta um2 ON um2.user_id = $wpdb->users.ID WHERE (um1.meta_key = 'uRPhICRS_capabilities' AND um1.meta_value LIKE '%agent%') AND ((um2.meta_key = 'first_name' OR um2.meta_key = 'last_name') AND um2.meta_value LIKE '%".$search_term."%');");
if(!empty($the_users)){
return $the_users;
}
else{
return false;
}
}
Two Ways as follow:
1) If You want to check from the string match in either firstname or lastname, following will be work:
$users_query = new WP_User_Query(
array(
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'first_name',
'value' => $str,
'compare' => 'LIKE'
),
array(
'key' => 'last_name',
'value' => $str,
'compare' => 'LIKE'
)
)
)
);
$users = $users_query->get_results();
2) If you want to check the user from username, user_nicename, user_email, firstname, or lastname use following code:
//search from the user table
$users_query_table = new WP_User_Query(
array(
'search' => "*{$str}*",
'search_columns' => array(
'user_login',
'user_nicename',
'user_email',
),
) );
$users_via_table = $users_query_table->get_results();
//search from the usermeta
$users_query_meta = new WP_User_Query(
array(
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'first_name',
'value' => $str,
'compare' => 'LIKE'
),
array(
'key' => 'last_name',
'value' => $str,
'compare' => 'LIKE'
)
)
)
);
$users_via_meta = $users_query_meta->get_results();
// Merge both result..
$combined_users = array_merge( $users_via_table, $users_via_meta );
// Get unique user
$users = array_unique( $combined_users, SORT_REGULAR );
You can use WP_User_Query.
Example:
// The search term
$search_term = 'Ross';
// WP_User_Query arguments
$args = array (
'role' => 'reporter',
'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);
https://developer.wordpress.org/reference/classes/wp_user_query/

Wordpress - WP Query : meta_query relation ignored

I need help with a WP_Query. I use the meta query argument using the OR and AND relations, but this argument seems to be ignored in the result of the query.
Here is my code :
$args = array(
'orderby' => 'date',
'order' => 'DESC',
'post_type' => 'my_custome_post_type',
'posts_per_page' => 1,
'meta_query' => array(
'relation' => 'OR',
array(
'relation' => 'AND',
array( 'author' => $contact_id ),
array( 'meta_key' => 'my_meta', 'meta_value' => $user_id )
),
array(
'relation' => 'AND',
array( 'author' => $user_id ),
array( 'meta_key' => 'my_meta', 'meta_value' => $contact_id )
)
)
);
$query = new \WP_Query( $args );
$response = $query->posts;
I already tried to add this argument like suggested in here:
'suppress_filters' => false,
'is_your_custom_query' => true, // your custom flag
Even if I remplace the value of $user_id and $contact_id directly in the query with any number, the query still return the same result. I don't understand why it's not working.
Thank you for your help !
As suggested by dafoxuk, I have to remplace meta_key by key and meta_value by value. I also had to add 'compare' => 'LIKE'.
But event in this case it wasn't working. I also had to stock the author_id in a post_meta, and change the array( 'author'=> $contact_id ) condition by :
array( 'key ' => 'meta_author_id', 'value' => $user_id, 'compare' => 'LIKE' )
So the final $args array look like this :
$args = array(
'orderby' => 'date',
'order' => 'DESC',
'post_type' => 'my_custome_post_type',
'posts_per_page' => 1,
'meta_query' => array(
'relation' => 'OR',
array(
'relation' => 'AND',
array( 'key ' => 'meta_author_id', 'value' => $user_id, 'compare' => 'LIKE' )
array( 'key ' => 'my_meta', 'value' => $user_id, 'compare' => 'LIKE '
array(
'relation' => 'AND',
array( 'key ' => 'meta_author_id', 'value' => $user_id, 'compare' => 'LIKE' )
array( 'key ' => 'my_meta', 'value' => $contact_id, 'compare' => 'LIKE' )
)
);

How to order posts list using wp_query?

I want to order my list with wp_query. I have posts with meta_key 'featured_post' and three values for that key for different posts it is: '1', '2', and thrird key not exist.
I am using now this wp_query structure:
global $paged, $wp_query, $wp;
$args = wp_parse_args($wp->matched_query);
if ( !empty ( $args['paged'] ) && 0 == $paged ) {
$wp_query->set('paged', $args['paged']);
$paged = $args['paged'];
}
$temp = $wp_query;
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => 20,
'paged' => $paged,
'meta_query' => array(
'relation' => 'OR',
'featured_clause_first' => array(
'key' => 'featured_post',
'value' => '1',
'compare' => '='
),
'featured_clause_second' => array(
'key' => 'featured_post',
'value' => '2',
'compare' => '='
),
'featured_clause_third' => array(
'key' => 'featured_post',
'compare' => 'NOT EXISTS',
'value' => '1'
)
),
'orderby' => array(
'featured_clause_first' => 'DESC',
'featured_clause_second' => 'DESC',
'featured_clause_third' => 'DESC'
),
);
$wp_query = null;
$wp_query = new WP_Query($args);
while ($wp_query->have_posts()) : $wp_query->the_post();
get_template_part( 'templates/single');
endwhile;
wp_reset_postdata();
wp_reset_query();
But I am got wrong order. Finally I need to see posts with value '1' at the top and other posts with values '2' and not exist key after.
Note: I can't use "'meta_key' => 'featured_post'," because in this way posts without existing meta_key not displayed.
Ok. I was edited save metabox functions for delete metakey if featured not checked. So that I have only two values: 1 & key not exist. After that I got needed order with this meta args:
$sort_meta_query = array(
'relation' => 'OR',
'featured_clause' => array(
'key' => 'featured_post',
'value' => '1',
'compare' => '='
),
array(
'key' => 'featured_post',
'compare' => 'NOT EXISTS',
'value' => '1'
)
);
$sort = array(
'orderby' => array(
'featured_clause' => 'DESC',
'date' => 'DESC'
)
);

Get all wp_users sort by metakey

I am trying to get users list sort by metakey. I mean i have set meta key user_last_login. some users doesnot have this meta key. some users have it.
I want to get both users in single query but if metakey exists those will come first and remaining should come last.
Here is my query
$meta_query_args = array(
'relation' => 'OR', // Optional, defaults to "AND"
array(
'key' => 'user_last_login',
'compare' => 'Not Exists'
),
array(
'key' => 'user_last_login',
'compare' => 'Exists'
)
);
$meta_query = new WP_Meta_Query( $meta_query_args );
$args = array(
'offset' => $paged ? ($paged - 1) * $number : 0,
'number' => $number,
'fields' => 'all',
'orderby'=>'user_last_login',
'exclude' => array(get_current_user_id()),
'meta_query' =>$meta_query
);
$users = get_users( $args );
Please help.
A meta_key argument is required when you wan to order by meta_value. Here is the modified code,
$query_args = array(
'relation' => 'OR',
array(
'key' => 'user_last_login',
'compare' => 'NOT EXISTS'
),
array(
'key' => 'user_last_login',
'compare' => 'EXISTS'
)
);
$meta_query = new WP_Meta_Query($query_args);
$args = array(
'offset' => $paged ? ($paged - 1) * $number : 0,
'number' => $number,
'fields' => 'all',
'meta_key' => 'user_last_login',
'orderby' => 'meta_value',
'order' => 'ASC',
'exclude' => array(get_current_user_id()),
'meta_query' => $meta_query
);
$users = get_users( $args );
Hope this one helps.

How to search Wordpress User by Username or First name on frontend?

This is my code but didnt get desired result.I just want to search out the specific user by its first_name and its role is it_guy. Anyone please help.
$users = new WP_User_Query(
array(
'role' => 'it_guy',
'meta_key' => 'first_name',
'search' => '*'.esc_attr( $your_search_string ).'*',
'orderby' => 'first_name',
'order' => 'ASC',
'offset' => '',
'number' => '',
));
foreach($users as $user)
{
<?php echo $user->first_name; ?>
}
You should try this,
$users = new WP_User_Query(
array(
'role' => 'it_guy',
'meta_key' => 'first_name',
'orderby' => 'meta_value',
'order' => 'ASC',
'offset' => '',
'number' => '',
'meta_query' => array(
array(
'key' => 'first_name',
'value' => esc_attr( $your_search_string ),
'compare' => 'LIKE'
)
)
)
);

Resources