Wordpress WP_User_Query search - php

I am trying to write a user search query, i have location and searching keyword (user name)
i need to search all user, with last name or first name matches to searching keyword for a
specified loaction
my code here,
$args = array(
'meta_query' => array(
'relation' => 'AND',
0 => array(
'key' => 'last_name',
'value' => $search_string,
'compare' => 'LIKE'
),
1 => array(
'key' => 'first_name',
'value' => $search_string,
'compare' => 'LIKE'
),
2 => array(
'key' => 'user_city',
'value' => $Location,
'compare' => 'LIKE'
),
),
);
$users = new WP_User_Query( $args);
$users = $users->get_results();
but i want 'relation' OR for first name and last name and relation AND for location, how i can do it ?

please use
$args = array( 'meta_query' =>
array(
array(
'relation' => 'OR',
array( 'key' => 'last_name', 'value' => $search_string, 'compare' => 'LIKE' ),
array( 'key' => 'first_name', 'value' => $search_string, 'compare' => 'LIKE' ),
),
array( 'key' => 'user_city', 'value' => $Location, 'compare' => 'LIKE' ), ),
);

I believe it's not possible to have multiple relation fields in a wp_query. Please reference https://codex.wordpress.org/Class_Reference/WP_User_Query.
As an alternative, you can look into pre_user_query, which is a hook to call a function before the query is called.
add_filter( 'pre_user_query', 'some_function' );
function some_function () {
//do custom query altering here.
}

Try this
$user_ex = explode(' ', $search_string);
if($user_ex[1]) {
$name_array = preg_split("/[\s,]+/", $search_string_user);
$args = new WP_User_Query( array(
'meta_query' => array(
'relation' => 'OR',
array(
'relation' => 'OR',
array(
'key' => 'user_city',
'value' => $search_string,
'compare' => 'LIKE'
),
),
array(
'relation' => 'AND',
array(
'key' => 'first_name',
'value' => $name_array[0],
'compare' => 'LIKE'
),
array(
'key' => 'last_name',
'value' => $name_array[1],
'compare' => 'LIKE'
),
),
),
}

Related

How to build an array for a WP_User_Query

I have an array:
$args = array(
'orderby' => 'meta_value',
'meta_key' => $columnName,
'order' => $columnOrder,
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'shipping_company',
'value' => '^'.$prospectSearch,
'compare' => 'REGEXP',
),
array(
'key' => 'shipping_company',
'value' => '---NV',
'compare' => 'NOT LIKE',
),
array(
'key' => 'shipping_company',
'value' => '---DS',
'compare' => 'NOT LIKE',
),
array(
'key' => 'customer_status',
'value' => '1',
'compare' => '=',
),
),
);
Along with that I also need to search the user_meta table for many many zip codes in a loop like this:
$args['meta_query'][]=array(
'relation' => 'OR',
);
foreach($zips as $zip) {
$args['meta_query'][]=array(
array(
'key' => 'shipping_postcode',
'value' => $zip,
'compare' => '=',
),
);
}
The problem is that the 'OR' in the zip code portion always gets changed to an 'AND'. I feed this array to WP_User_Query like this:
$wpUserQuery = new WP_User_Query($args);
I use the following line to get the SQL:
$wpUserQuery->request;
I went a different route. Rather than using WP_User_Query I am going the build the SQL string manually and optimize it.

Wordpress | Split queries based on selection

I am implementing the filters for the content searched by users inside my ajax form in frontend, I am working with this query that works perfectly
$args = array(
'post_type' => 'pro',
'posts_per_page' => -1,
'meta_key' => $_POST['categoryfilter'],
'meta_query' => array(
'relation' => 'AND',
array(
'key' => $_POST['categoryfilter'],
'value' => 0,
'compare' => '>=',
'type' => 'NUMERIC',
),
array(
'key' => $_POST['categoryfilter'],
'value' => 100,
'compare' => '<=',
'type' => 'NUMERIC',
),
),
'orderby' => 'meta_value_num',
'order' => $_POST['order'],
);
my goal was to split the query based on the $_POST of the filter on the form
and I wanted to get that when the user selected the filter: firts, I split the above query into this one to get this
'meta_query' => array(
'relation' => 'AND',
array(
'key' => $_POST['categoryfilter'],
'value' => 0,
'compare' => '>=',
'type' => 'NUMERIC',
),
array(
'key' => $_POST['categoryfilter'],
'value' => 100,
'compare' => '<=',
'type' => 'NUMERIC',
),
),
so i tried with this code but it doesn't work:
if( isset($_POST['first'] ))
$args['meta_query'][] =
array(
'relation' => 'AND',
array(
'key' => $_POST['categoryfilter'],
'value' => 0,
'compare' => '>=',
'type' => 'NUMERIC',
),
array(
'key' => $_POST['categoryfilter'],
'value' => 50,
'compare' => '<=',
'type' => 'NUMERIC',
),
);
Where am I doing wrong?
same thing for sorting I would like when the user enters: order, be able to filter by ASC or DESC as in the first query
I think it's just an issue with how you create the meta_query. You are storing it in a sub-array, but it should not. Try to update like this:
if( isset($_POST['first'] ))
$args['meta_query'] =
array(
'relation' => 'AND',
array(
'key' => $_POST['categoryfilter'],
'value' => 0,
'compare' => '>=',
'type' => 'NUMERIC',
),
array(
'key' => $_POST['categoryfilter'],
'value' => 50,
'compare' => '<=',
'type' => 'NUMERIC',
),
);
When doing $args['meta_query'][] = you should push every array of the meta_query one by one, but here you already have the full meta_query.
This assuming you don't have more code that could push more meta_queries rules. If so, then you should push each part at a time.

WP_Query, using AND with OR

I have the below PHP:-
$args = array(
'posts_per_page'=> -1,
'post_type' => 'jobs',
'order' => 'ASC',
's' => $search_field,
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'job_salary_from',
'value' => $job_salary_from,
'compare' => '>=',
),
array(
'key' => 'job_salary_from',
'value' => $job_salary_to,
'compare' => '<=',
),
array(
'relation' => 'OR',
array(
'key' => 'job_salary_to',
'value' => $job_salary_from,
'compare' => '>=',
),
array(
'key' => 'job_salary_to',
'value' => $job_salary_to,
'compare' => '<=',
),
))
);
Lets say there is a job that is from 19000 to 22000 called 'ABC', so
job_salary_from = 19000
and job_salary_to = 22000.
Now lets say I search a job that is between 10000 and 19000, so $job_salary_from = 10000 and $job_salary_to = 19999.
This shows up when I do a search which is correct. However, if I search for a job that is between 20000 and 29999, so $job_salary_from = 19999 and $job_salary_to = 29999 nothing is showing up.
Where as the job 'ABC' should show up because $job_salary_to is within the search bracket.
Any help would be much appreciated.
I think the meta_query syntax is not quite right. Try this:
$args = array(
'posts_per_page'=> -1,
'post_type' => 'jobs',
'order' => 'ASC',
's' => $search_field,
'meta_query' => array(
'relation' => 'OR',
array(
'relation' => 'AND',
array(
'key' => 'job_salary_to',
'value' => $job_salary_from,
'type' => 'numeric', //for each case
'compare' => '>=',
),
array(
'key' => 'job_salary_to',
'value' => $job_salary_to,
'type' => 'numeric',
'compare' => '<=',
),
),
array(
'relation' => 'AND',
array(
'key' => 'job_salary_from',
'value' => $job_salary_from,
'type' => 'numeric',
'compare' => '>=',
),
array(
'key' => 'job_salary_from',
'value' => $job_salary_to,
'type' => 'numeric',
'compare' => '<=',
),
)
)
);
Also you can try to use BETWEEN, even I am not sure if this is inclusive or exclusive compare.
that is because there is a different function to fetch results in between a range.
Try the following example to fetch results in between a range
$args = array(
'post_type' => 'product',
'meta_query' => array(
array(
'key' => 'color',
'value' => 'blue',
'compare' => 'NOT LIKE',
),
array(
'key' => 'price',
'value' => array( 20, 100 ),
'type' => 'numeric',
'compare' => 'BETWEEN',
),
),
);
$query = new WP_Query( $args );
More resources here https://codex.wordpress.org/Class_Reference/WP_Query

Wordpress meta_query multiple fields search NOT working

I have this piece of code and it works just fine:
$search = array(
'meta_query' => array(
array(
'key' => 'wpcf-community-city',
'value' => $search_param,
'compare' => 'LIKE'
)
)
);
But when I change to this, it stops working.
$search = array(
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'wpcf-community-city',
'value' => $search_param,
'compare' => 'LIKE'
),
array(
'key' => 'wpcf-community-state',
'value' => $search_param,
'compare' => 'LIKE'
),
array(
'key' => 'wpcf-community-zip',
'value' => $search_param,
'compare' => 'LIKE'
)
)
);
I'm using Wordpress 3.4.2
PS: This piece of code is a part of the query_posts() parameters.
Why u dont use WP_Query ?
U need use the 'relation' parameter ?
Use this example.. its work:
'meta_query' => array(
array(
'key' => 'color',
'value' => 'blue',
'type' => 'CHAR',
'compare' => '=' //'=', '!=', '>', '>=', '<', '<=', 'LIKE', 'NOT LIKE', 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN'
),
array(
'key' => 'price',
'value' => array( 1,200 ),
'compare' => 'NOT LIKE'
)
),

Custom Search Query in relation to search form

I've got some custom fields as meta data for a new post type - Properties (for an estate agents), so want to search by number of bedrooms, min/max value and location. I have a form with multiple drop-downs for each of these fields:
location, min_value, max_value, bedrooms
Also, I have meta boxes on the posts themselves, so one for price, bedrooms, location, and a taxonomy type of property_type - rent, sale, and commerical.
I've found this piece of code online but not sure how to manipulate it so it takes whatever value the form takes?
$args = array(
'post_type' => 'product',
'meta_query' => array(
array(
'key' => 'location',
'value' => '[LOCATION HERE]',
'compare' => 'NOT LIKE'
),
array(
'key' => 'price',
'value' => '[PRICE HERE FROM FORM]',
'type' => 'numeric',
'compare' => 'BETWEEN'
)
)
);
$query = new WP_Query( $args );
Also, I understand the search query goes on function.php but do I call it from where the form is, or where the results are outputted? ie. my homepage or my searchpage?
Hope someone can help
use this code
$args = array(
'post_type' => 'Properties',
'meta_query' => array(
array(
'key' => 'location',
'value' => '[LOCATION HERE]',
'compare' => 'LIKE'
),
array(
'key' => 'min_value',
'value' => '[min value here]',
'type' => 'numeric',
'compare' => 'BETWEEN'
)
array(
'key' => 'max_value',
'value' => '[max value here]',
'type' => 'numeric',
'compare' => 'BETWEEN'
)
array(
'key' => 'bedrooms',
'value' => '[bedroom here]',
'compare' => 'LIKE'
),
)
);
$query = new WP_Query( $args );
and You have to call this in your searchpage....
Thanks for help Yogesh, I modified your answer to get this which seems to work:
<?php $args = array(
'post_type' => 'Property',
'property_type'=>$_GET['type'],
'meta_query' => array(
'relation' => 'AND',
array(
'key' => '_property_info_location',
'value' => Cuztom::uglify($_GET['location']),
),
array(
'key' => '_property_info_bedrooms',
'value' => $_GET['bedrooms'],
),
array(
'key' => '_property_info_price',
'value' => $_GET['max_value'],
'compare' => '<=',
'type' => 'numeric',
),
array(
'key' => '_property_info_price',
'value' => $_GET['min_value'],
'compare' => '>=',
'type' => 'numeric',
),
),
);
$the_query = new WP_Query( $args );
?>

Categories