I am working on custom search by meta value and using this:
print_r($_REQUEST);
$args = array(
'post_type' => 'property_post',
'posts_per_page' => 10,
'order' => 'ASC',
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'custom_textarea',
'value' => 'me', // if I use static keyword it works
'compare' => 'LIKE'
),
array(
'key' => 'custom_price',
'value' => array( $_REQUEST['custom_price'], $_REQUEST['custom_price1'] ),
'type' => 'numeric',
'compare' => 'BETWEEN'
),
array(
'key' => 'custom_beds',
'value' => $_REQUEST['custom_beds'],
'compare' => '='
),
array(
'key' => 'custom_bath',
'value' => $_REQUEST['custom_bath'],
'compare' => '='
),
array(
'key' => 'custom_garage',
'value' => $_REQUEST['custom_garage'],
'compare' => '='
)
)
);
If I use some static keyword for meta value then it works but with the $_REQUEST it did not.
I checked $_REQUEST by print_r($_REQUEST).
Array ( [custom_textarea] => aa[custom_price] => 1000 [custom_price1] => 4000[custom_beds] => 2[custom_garage] => 1)
So what should i do to make it fine.
Thanks in advance.........
Store the $_REQUEST values to some variables and use it in the query
$a=$_REQUEST['custom_price'];
and
'value' => $a,
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.
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 this code:
$args3 = array(
'post_type' => 'imoveis_a_venda',
'cat' => $categoria,
'meta_query' => array(
array(
'key' => 'valor',
'value' => array($amount1,$amount2),
'compare' => 'BETWEEN',
'type' => 'NUMERICAL'
),
)
);
to values between 0 and 999.999,00 works perfectly.
But when passes 1 million does not work.
Set type in meta_query to SIGNED.
$args3 = array(
'post_type' => 'imoveis_a_venda',
'cat' => $categoria,
'meta_query' => array(
array(
'key' => 'valor',
'value' => array($amount1,$amount2),
'compare' => 'BETWEEN',
'type' => 'SIGNED'
),
)
);
I'm trying to apply a query, in order to display posts from different custom fields (with the same input - $id)
When testing the code below, I get the whole website to crash...
If I remove the name2 & name3 arrays, it works just fine.
Does anyone have a clue where I got stupid this time?
Thanks!
$example = new WP_Query(array(
'post_type' => 'example',
'paged' => $paged,
'posts_per_page' => '-1',
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'name1',
'value' => $id,
'type' => 'numeric',
'compare' => '='
),
array(
'key' => 'name2',
'value' => $id,
'type' => 'numeric',
'compare' => '='
),
array(
'key' => 'name3',
'value' => $id,
'type' => 'numeric',
'compare' => '='
)
)
));