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

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/

Related

Wordpress meta query not working with dynamic parameters

I'm trying to make a call using a custom end-point to one custom post type on my project.
I'm passing dynamic parameters to that function-
// $per_page = $_GET['per_page'];
// $page = $_GET['page'];
$per_page = 18;
$page = 1;
$type = $_GET['type'];
$order = $_GET['order'];
// $houseType = $_GET['houseType'];
$houseType = 'Terreno';
$houseState = $_GET['houseState'];
$houseArea = $_GET['houseArea'];
$housePrice = $_GET['housePrice'];
$houseRooms = $_GET['houseRooms'];
// $houseProp = $_GET['houseProp'];
$houseProp = 'venda';
$meta_query = array();
if( $houseProp ) {
$meta_query[] = array(
'key' => 'tipo_de_operacao',
'value' => $houseProp,
'compare' => '=',
);
}
if ( $houseType ) {
$meta_query[] = array(
'key' => 'tipo_de_imovel',
'value' => $houseType,
'compare' => '=',
);
}
if ( $houseState ) {
$meta_query[] = array(
'key' => 'estado_do_imovel',
'value' => $houseState,
'compare' => '=',
);
}
if ( $houseArea ) {
$meta_query[] = array(
'key' => 'area_bruta',
'value' => $houseArea[0],
'type' => 'NUMERIC',
'compare' => '>'
);
$meta_query[] = array(
'key' => 'area_bruta',
'value' => $houseArea[1],
'type' => 'NUMERIC',
'compare' => '<'
);
}
if ( $housePrice ) {
$meta_query[] = array(
'key' => 'preco',
'value' => $housePrice[0],
'type' => 'NUMERIC',
'compare' => '>'
);
$meta_query[] = array(
'key' => 'preco',
'value' => $housePrice[1],
'type' => 'NUMERIC',
'compare' => '<'
);
}
if ( $houseRooms ) {
$meta_query[] = array(
'key' => 'quartos',
'value' => $houseRooms[0],
'type' => 'NUMERIC',
'compare' => '>'
);
$meta_query[] = array(
'key' => 'quartos',
'value' => $houseRooms[1],
'type' => 'NUMERIC',
'compare' => '<'
);
}
if ( $type == "preco") {
$orderBy = array(
'meta_key' => $type,
'orderby' => 'meta_value_num',
'order' => $order,
);
} else {
$orderBy = array(
'orderby' => array(
'date' => $order,
'menu_order'=> $order,
)
);
}
$args = array_merge($orderBy, array(
'post_type' => 'imoveis',
'posts_per_page' => $per_page,
'paged' => $page,
'meta_query' => $meta_query,
));
// get posts
$posts = get_posts($args);
The problem is that the $meta_query is not working...
If I pass only 1 parameter (houseProp), I can make that work with $meta_query[0], but if I pass more parameters it does not return anything.
What am I doing wrong?
EDIT:
I was able to see that I need to update my post on the backend (by pressing the update button).
I'm creating a post like this:
$post_id = wp_insert_post(array(
'post_title' => $house->InfoGeral->SubTipoImovel,
'post_type' => 'imoveis',
'post_content' => $description[0],
'post_status' => 'publish')
);
and filling the ACF custom fields like this:
update_field('field_5d230e2057fa5', $ref, $post_id);
what could possibly be the error?
When you use $meta_query it uses the 'relation' attribute and is defaulted to 'AND'. For example, when you set it up with one parameter, like your example:
$meta_query[] = array(
'key' => 'tipo_de_operacao',
'value' => $houseProp,
'compare' => '=',
);
You are really setting it to look like:
$meta_query = array(
'relation' => 'AND',
array(
'key' => 'tipo_de_operacao',
'value' => $houseProp,
'compare' => '=',
)
);
So now when you add a bunch of other parameters, it really looks like this:
$meta_query = array(
'relation' => 'AND',
array(
'key' => 'tipo_de_operacao',
'value' => $houseProp,
'compare' => '=',
),
array(
'key' => 'area_bruta',
'value' => $houseArea[0],
'type' => 'NUMERIC',
'compare' => '>'
),
array(
'key' => 'area_bruta',
'value' => $houseArea[1],
'type' => 'NUMERIC',
'compare' => '<'
)
);
So my guess is you are starting to really narrow your results down to this AND that AND this other thing, which is probably too restrictive. You can change the relation attribute to 'OR', if that is your goal, or attempt to re-code it to be less restrictive.
You can fix this by changing your line of:
$meta_query = array();
to:
$meta_query = array( 'relation' => 'OR' );
and keep everything else the same.

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' )
)
);

can't search by email 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.

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 do I pass two meta value to WP_Query

I'm having issues with my WP_Query. So I have two select boxes created using ACF one for Country and one for sector. When I select country and leave sector empty I get all the posts with the selected country and like wise for the sector. What I need to do is filter it further so if both are selected and not just one I get all posts from that country and sector. My args below any help is appreciated.
$field_key_country = "field_57b4439b5e371";
$field_country = get_field_object($field_key_country);
$country_sector = isset($_POST['country_select'])? sanitize_text_field($_POST['country_select']) : false;
$sector_key = "field_57e15152d896d";
$sector_field = get_field_object($sector_key);
$sector = isset($_POST['sector_select'])? sanitize_text_field($_POST['sector_select']) : false;
$args = array(
'post_type' => 'distributer_search',
'posts_per_page' => -1,
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'dis_country',
'value' => $country_sector,
),
array(
'key' => 'dis_sector',
'value' => $sector,
)
)
);
May be it's helpful for you -
$field_key_country = "field_57b4439b5e371";
$field_country = get_field_object($field_key_country);
$country_sector = isset($_POST['country_select'])? sanitize_text_field($_POST['country_select']) : false;
$sector_key = "field_57e15152d896d";
$sector_field = get_field_object($sector_key);
$sector = isset($_POST['sector_select'])? sanitize_text_field($_POST['sector_select']) : false;
$args = array(
'post_type' => 'distributer_search',
'posts_per_page' => -1
);
if($country_sector){
$args['meta_query'][] = array(
'key' => 'dis_country',
'value' => $country_sector
);
}
if($sector){
$args['meta_query'][] = array(
'key' => 'dis_sector',
'value' => $sector
);
}
Please try following code with relation AND & compare operator.
Also please check WP_Query class at https://codex.wordpress.org/Class_Reference/WP_Query
<?php
$country_sector = isset($_POST['country_select'])? sanitize_text_field($_POST['country_select']) : '';
$sector = isset($_POST['sector_select'])? sanitize_text_field($_POST['sector_select']) : '';
$args = array(
'post_type' => 'distributer_search',
'posts_per_page' => -1,
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'dis_country',
'value' => $country_sector,
'compare' => '=',
),
array(
'key' => 'dis_sector',
'value' => $sector,
'compare' => '=',
)
)
);
?>
'distributer_search',
'posts_per_page' => -1,
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'dis_country',
'value' => $country_sector,
'compare' => '=',
),
array(
'key' => 'dis_sector',
'value' => $sector,
'compare' => '=',
)
)
);
?>

Resources