I am working on a 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 $_REQUEST
it doesn’t. I checked $_REQUEST
using 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 work?
First of all, one never should rely on raw
$_REQUEST
: believe me, this is a security issue.PHP has 2 functions:
filter_input
andfilter_input_array
that helps you to sanitize your request data.In addition, in your request dump I don’t see any
$_REQUEST['custom_bath']
, but you are using that value, that can be a cause for the issue. You should check that a variable is setted before use it.Using my suggestions your code becomes something like this
The problem is when you have a string variable WordPress doesn’t automatically add quotes to the variable and therefore the search filter doesn’t generate any result.
I suggest the following answer.
And then your
meta_query
would be:For mine, I use it like so on search results page:
then in the query…
Does this help? Try using $_POST rather than $_REQUEST?