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
Related
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.
$tax_query = array(
array(
'taxonomy' => 'category',
'field' => 'id',
'terms' => $ajs_cat,
)
);
$args = array(
'post_type' => 'post',
'orderby' => 'menu_order',
'posts_per_page' => $per_page,
'paged' => $paged,
'tax_query' => $tax_query,
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'price_min',
'value' => array($min_price, $max_price),
'type' => 'NUMERIC',
'compare' => 'BETWEEN'
),
array(
'key' => 'price_max',
'value' => array($min_price, $max_price),
'type' => 'NUMERIC',
'compare' => 'BETWEEN'
),
)
);
I am trying to return acts based on a searched budget minimum (price_min) and maximum (price_max).
So if act 'X' has a minimum fee of 3000 (price_min) and a maximum fee of 6000 (price_max)
Search A: ('X' is in results)
I search with a budget of minimum of 3000 ($min_price) and a maximum of 5500 ($max_price) i get 'X' returned in the results.
Search B: ('X' is not in results)
I search with a budget of minimum of 3500 ($min_price) and a maximum of 5500 ($max_price) i would expect to see 'X' returned in the results as i could still afford him given my budget.
I need 'X' to return in both instances
Could someone help point me in the right direction?
shouldn't it be
array(
'key' => 'price_min',
'value' => $min_price,
'type' => 'NUMERIC',
'compare' => 'BETWEEN'
),
array(
'key' => 'price_max',
'value' => $max_price,
'type' => 'NUMERIC',
'compare' => 'BETWEEN'
If you selected both minimum and maximum price than your query like ::
array(
'key' => 'meta_key_min_price',
'value' => array( $min_p, $max_p ),
'type' => 'numeric',
'compare' => 'BETWEEN',
),
array(
'key' => 'meta_key_max_price',
'value' => array( $min_p, $max_p ),
'type' => 'numeric',
'compare' => 'BETWEEN',
),
If you select only one of them than your query look like this ::
For Minimum
array(
'key' => 'meta_key_min_price',
'value' => $min_p,
'type' => 'numeric',
'compare' => '>=',
),
For Maximum
array(
'key' => 'meta_key_max_price',
'value' => $min_p,
'type' => 'numeric',
'compare' => '<=',
),
$tax_query = array(
array(
'taxonomy' => 'category',
'field' => 'id',
'terms' => $ajs_cat,
)
);
$args = array(
'post_type' => 'post',
'orderby' => 'menu_order',
'posts_per_page' => $per_page,
'paged' => $paged,
'tax_query' => $tax_query,
'meta_query' => array(
array(
'key' => 'price_min',
'value' => $min_price,
'compare' => '>='
),
array(
'key' => 'price_max',
'value' => $max_price,
'compare' => '<='
),
)
);
We have set up custom post types for real estate properties and a search form to search through these custom post types using price, city, newest, etc. We are using query variable strings/if statements to display the results, but we are having trouble with the sort options. We have three sort options in place : Newest, Price Low-High, and Price High-Low.
When the default results are displayed, the sort does not work correctly, but when a city is set in the search dropdown, the sort works perfectly.
For the city search, we have this code in place:
if (isset($_POST['city'])) {
$qs_city = get_query_var('city');
}
else {
$qs_city = " ";
I believe that this is due to the else portion of the code above. We need some sort of wildcard that allows all cities listed to be listed if a city is not set. We have tried several different things, but nothing seems to work. I'm fairly new to this, so I can better clarify any of the above, if necessary.
This is the code used to sort:
if (isset($_POST['sort'])) {
$qs_sort = get_query_var('sort');
switch ($qs_sort) {
case 'newest':
$args = array(
'post_type' => 'residential',
'posts_per_page' => 10,
'paged' => $paged,
'orderby' => 'L_StatusDate',
'order' => 'DESC',
'meta_query' => array(
array(
'key' => 'L_City',
'value' => $qs_city,
'compare' => 'LIKE'
),
array(
'key' => 'L_SystemPrice',
'value' => array( $qs_price_min, $qs_price_max ),
'type' => 'numeric',
'compare' => 'BETWEEN'
),
array(
'key' => 'LM_Int1_2',
'value' => $qs_beds,
'compare' => '>='
),
array(
'key' => 'LM_Int1_3',
'value' => $qs_baths,
'compare' => '>='
)
)
);
break;
case 'price-low':
$args = array(
'post_type' => 'residential',
'posts_per_page' => 10,
'paged' => $paged,
'orderby' => 'L_SystemPrice',
'order' => 'ASC',
'meta_query' => array(
array(
'key' => 'L_City',
'value' => $qs_city,
'compare' => 'LIKE'
),
array(
'key' => 'L_SystemPrice',
'value' => array( $qs_price_min, $qs_price_max ),
'type' => 'numeric',
'compare' => 'BETWEEN'
),
array(
'key' => 'LM_Int1_2',
'value' => $qs_beds,
'compare' => '>='
),
array(
'key' => 'LM_Int1_3',
'value' => $qs_baths,
'compare' => '>='
)
)
);
break;
case 'price-high':
$args = array(
'post_type' => 'residential',
'posts_per_page' => 10,
'paged' => $paged,
'orderby' => 'L_SystemPrice',
'order' => 'DESC',
'meta_query' => array(
//'relation' => 'AND',
array(
'key' => 'L_City',
'value' => $qs_city,
'compare' => 'LIKE'
),
array(
'key' => 'L_SystemPrice',
'value' => array( $qs_price_min, $qs_price_max ),
'type' => 'numeric',
'compare' => 'BETWEEN'
),
array(
'key' => 'LM_Int1_2',
'value' => $qs_beds,
'compare' => '>='
),
array(
'key' => 'LM_Int1_3',
'value' => $qs_baths,
'compare' => '>='
)
)
);
break;
}
}
else {
$args = array(
'post_type' => 'residential',
'posts_per_page' => 10,
'paged' => $paged,
'meta_query' => array(
//'relation' => 'AND',
array(
'key' => 'L_StatusDate',
'orderby' => 'DESC'
//set default to sort by newest
),
array(
'key' => 'L_City',
'value' => $qs_city,
'compare' => 'LIKE'
),
array(
'key' => 'L_SystemPrice',
'value' => array( $qs_price_min, $qs_price_max ),
'type' => 'numeric',
'compare' => 'BETWEEN'
),
array(
'key' => 'LM_Int1_2',
'value' => $qs_beds,
'compare' => '>='
),
array(
'key' => 'LM_Int1_3',
'value' => $qs_baths,
'compare' => '>='
)
)
);
}
?>
The main question for your situation is "what kind of application will sort the results?". It looks like you use some kind on database. But which one? If it's SQL-like database, - just replace $qs_city = " " with $qs_city = "%";
I have price search in wp query like that
$args=array("post_type"=>"nemovitosti",'meta_query'=> array("key"=>"cena","value"=>array(0,1000),'type' => 'numeric','compare' => 'BETWEEN'),'offset'=>0,'posts_per_page'=>-1);
But what I need is find between more values like 0-1000, 5000-20000, 50000-200000 for key "cena".
How I can use wp_query for same key with another between values? Thanks!
Try with this:
<?php
$args = array(
'post_type' => 'nemovitosti',
'offset' => 0,
'posts_per_page' => 999,
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'cena',
'value' => array( 0, 1000),
'type' => 'numeric',
'compare' => 'BETWEEN',
),
array(
'key' => 'cena',
'value' => array( 5000, 20000),
'type' => 'numeric',
'compare' => 'BETWEEN',
),
array(
'key' => 'cena',
'value' => array( 50000, 200000),
'type' => 'numeric',
'compare' => 'BETWEEN',
),
),
);
This should target either prices between 0-1000, or 5000-20000 or 50000-200000.
Read more here WP_Query Custom_Field_Parameters.
I have the below PHP code:-
$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' => array($job_salary_from,$job_salary_to),
'type' => 'numeric',
'compare' => 'BETWEEN',
),
),
array(
'relation' => 'AND',
array(
'key' => 'job_salary_from',
'value' => array($job_salary_from,$job_salary_to),
'type' => 'numeric',
'compare' => 'BETWEEN',
),
)
)
);
This will check to see if the search is between the minimum and maximum values which all works perfectly okay.
Now I want to add a further query which will check if it matches up with a Job Sector, e.g. I added the following below 'meta_query':-
'relation' => 'AND',
array(
'key' => 'job_sector',
'value' => 'Finance',
'compare' => 'LIKE',
),
However, it seems it is just ignoring the above and I'm not sure why. Any help would be much appreciated!
So it looks like this now:-
<?php
$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' => array($job_salary_from,$job_salary_to),
'type' => 'numeric',
'compare' => 'BETWEEN',
),
),
array(
'relation' => 'AND',
array(
'key' => 'job_salary_from',
'value' => array($job_salary_from,$job_salary_to),
'type' => 'numeric',
'compare' => 'BETWEEN',
),
),
),
'relation' => 'AND',
array(
'key' => 'job_type',
'value' => $job_type,
'compare' => 'LIKE',
),
array(
'key' => 'job_location',
'value' => $job_location,
'compare' => 'LIKE',
)
);
$fetch_jobs = new WP_Query( $args );?>
The job_type / job_location conditions should be put under 'meta_query' I guess:
'meta_query' => array(
'relation' => 'AND',
array(
array(
'key' => 'job_type',
'value' => $job_type,
'compare' => 'LIKE',
),
array(
'key' => 'job_location',
'value' => $job_location,
'compare' => 'LIKE',
),
),
array(
'relation' => 'OR',
array(
'relation' => 'AND',
array(
'key' => 'job_salary_to',
'value' => array($job_salary_from,$job_salary_to),
'type' => 'numeric',
'compare' => 'BETWEEN',
),
),
array(
'relation' => 'AND',
array(
'key' => 'job_salary_from',
'value' => array($job_salary_from,$job_salary_to),
'type' => 'numeric',
'compare' => 'BETWEEN',
),
),
)
)