I have a meta_query that gets all the items with a price range between 2 variables. My problem is that 10 000 is not the same as 10000. How can I fix this?
I have been googling but I’n not sure what to google so I’m not really getting anywhere.
Help would be appreciated
Thanks
Hanè
Edit – Code Added
$kw_min = mysql_real_escape_string($_GET['kw_min']);
$kw_max = mysql_real_escape_string($_GET['kw_max']);
$pr_min = mysql_real_escape_string($_GET['pr_min']);
$pr_max = mysql_real_escape_string($_GET['pr_max']);
if ( !empty( $kw_min ) && !empty( $kw_max ) && !empty( $pr_min ) && !empty( $pr_max ) ) {
$args = array(
'post_type' => 'exc_agriq_equipment',
'exc_equipment_cat' => $term->slug,
'order' => 'DESC',
'posts_per_page' => 12,
'paged'=>$paged,
'meta_query' => array(
array(
'key' => 'exc_agriq_equipment_kw',
'value' => array( $kw_min, $kw_max ),
'type' => 'numeric',
'compare' => 'BETWEEN'
),
array(
'key' => 'exc_agriq_equipment_price',
'value' => array( $pr_min, $pr_max ),
'type' => 'numeric',
'compare' => 'BETWEEN'
),
),
);
}
The problem is that you’re comparing
10000
and10 000
as strings, and as such they are not the same value.So, you either need to sanitize the strings to ensure that both return an equivalent value, or you’ll need to force them to be integers, so that they’ll be evaluated as the same value.
Edit
Your best solution is probably to replace your text form field with a select, that includes pre-determined entry values (e.g. 10,000, 20,000, 30,000, etc.). That way, you will have reliable data to use in your
meta_query
.Well, “10 000” is not the same as “10000”. “10 000” isn’t even a number, strictly speaking, as a space is not a digit. In PHP,
(int)"10 000"
returns10
so you can’t even force it reliably. WordPress’sabsint
does the same. There may be a built-in function that will handle that in a human-like way but I’d have to look around for it.I would recommend normalizing your data as it goes into the database, as it is inserted, so that you don’t have to do any data manipulation as you are trying to retrieve the information. That is, figure out what your data should look like and make sure it fits before you insert it. If your prices are numbers, as I assume they are, check your data for anything that isn’t a digit and either reject it (if you are paranoid) or remove everything that isn’t a digit, then insert that. Something like…
$price = preg_replace('/[^0-9]/','',"$price");
Now when you need to search you know what your format is and you can do the same thing to any user supplied string before you send it to the database query, or reject data that doesn’t match (if you are paranoid, which I usually am, by the way). That should get you much more reliable results.
You can use MySQL’s
REPLACE
when you retrieve the data but I don’t think you can do that using core functions, unless you start messing with query filters, but that is really the backwards to do it.Preparing prices
WP has some stuff built in, as well as PHP has. The really tricky stuff is to regard all possible user behavior. You won’t catch everything, but you can catch alot, like unintentional white spaces, currency signs, etc. The function below is well commented and will show you how I think the user experience can be made better. I assume that your users will have a similar language as the one set in your CMS.
Then simply run the function on all needed values.