How to build an array for a WP_User_Query - php

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.

Related

How to write Wordpress Meta Queries with "if then" sorting logic

Summary
In my Wordpress build, I have a calendar made of custom event posts. These posts have two settings, single day event, and multiple day event. This is set as a boolean. I need to grab these posts, and sort them based on their date.
Problem
The issue I'm facing is that when I query the back-end, I don't know which date to sort by. I've tried using the boolean to create a sort of "if then" logic, but no luck.
Trouble Shooting
What have I tried so far?
This is the query I've been trying to get to work. No luck though
$events_query = new WP_Query (array(
'post_type' => 'events',
'posts_per_page' => '-1',
'meta_query' => array (
'relation' => 'AND',
array (
'key' => 'multiple_day_event',
'value' => '1',
'compare' => '=',
),
array (
'key' => 'start_date',
'value' => date("Ymd"),
'compare' => '>',
),
'relation' => 'OR',
array (
'key' => 'multiple_day_event',
'value' => '0',
'compare' => '=',
),
array (
'key' => 'date',
'value' => date("Ymd"),
'compare' => '>',
)
)
));
Can I use a single date instead of two separate dates?
No, not to my knowledge. Using a single date in place of both has causing other issues in my back-end.
Solution
In short, I need a query that's able to use OR, AND, OR logic. Any ideas?
According to the WP_Meta_Query docs:
Nested arrays can be used to construct complex queries
$meta_query_args = array(
'relation' => 'OR', // Optional, defaults to "AND"
array(
'key' => '_my_custom_key',
'value' => 'Value I am looking for',
'compare' => '='
),
array(
'relation' => 'AND',
array(
'key' => '_my_custom_key_2',
'value' => 'Value I am looking for 2',
'compare' => '='
),
array(
'key' => '_my_custom_key_3',
'value' => 'Value I am looking for 3',
'compare' => '='
)
)
);
$meta_query = new WP_Meta_Query( $meta_query_args );
So your example would look something like this:
$events_query = new WP_Query( array(
'post_type' => 'events',
'posts_per_page' => '-1',
'meta_query' => array (
'relation' => 'AND',
array(
'relation' => 'AND',
array (
'key' => 'multiple_day_event',
'value' => '1',
'compare' => '=',
),
array (
'key' => 'start_date',
'value' => date("Ymd"),
'compare' => '>',
),
),
array(
'relation' => 'OR',
array (
'key' => 'multiple_day_event',
'value' => '0',
'compare' => '=',
),
array (
'key' => 'date',
'value' => date("Ymd"),
'compare' => '>',
)
)
)
) );

Wordpress meta_query OR/AND relations not working

I have a search form with multiple options that are taxonomies. When I select all options, or just one, and click search it never returns any record. I have a problem with understanding relations, and how to properly set arrays.
$args = array('post_type' => array( 'property' ),
'order' => 'DESC',
'orderby' => 'post_date',
'meta_query' => array(
'relation' => 'AND',
array(
array(
'key' => 'property_address_postal_code',
'value' => $search,
'compare' => 'LIKE',
),
array(
'key' => 'property_address_state',
'value' => $search,
'compare' => 'LIKE',
),
array(
'key' => 'property_location',
'value' => $search,
'compare' => 'LIKE',
),
),
array(
'relation' => 'OR',
array(
'relation' => 'AND',
array(
'key' => 'property_price',
'value' => array($_GET['min-price'],$_GET['max-price']),
'type' => 'numeric',
'compare' => 'BETWEEN',
),
),
array(
'relation' => 'AND',
array(
'key' => 'property_price',
'value' => array($_GET['min-price'],$_GET['max-price']),
'type' => 'numeric',
'compare' => 'BETWEEN',
),
),
)
)
);
First off, I'm not sure if you're quite understanding how the relation property applies in this function. It applies AND/OR to other properties on the same level. So your very first relation => AND makes sure your $search variable matches one of the properties you specify AND that the property_price is between the min and max values.
Secondly, with subsequent, nested meta queries, the default relation is AND, so when you try to match $search against different properties, you're actually trying to make sure that $search is matching ALL of the properties you specify. You can fix that by trying to match $search against one property OR another property.
Thirdly, with your comparison of the price against the min and max price, the meta query is malformed, and both of those meta queries are identical. In that case, you don't need a relation since you only have to check against one value.
You can simplify your code as such:
$args = array('post_type' => array( 'property' ),
'order' => 'DESC',
'orderby' => 'post_date',
'meta_query' => array(
'relation' => 'AND',
array(
'relation' => 'OR',
array(
'key' => 'property_address_postal_code',
'value' => $search,
'compare' => 'LIKE',
),
array(
'key' => 'property_address_state',
'value' => $search,
'compare' => 'LIKE',
),
array(
'key' => 'property_location',
'value' => $search,
'compare' => 'LIKE',
),
),
array(
'key' => 'property_price',
'value' => array($_GET['min-price'],$_GET['max-price']),
'type' => 'numeric',
'compare' => 'BETWEEN',
)
)
);
Now, you're making a query that checks if $search is like property_address_potal_code OR like property_address_state OR like property_location AND property_price is between the min and max price.
You might still have to fiddle with it a bit depending on other logic in your code, but I think this should get you going in the right direction.

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 WP_User_Query search

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'
),
),
),
}

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

Categories