First of all i've found numerous, topics, and, issues about ordering a WP_Query
by its custom field value, but somehow all the proposed solutions don't seem to work for me.
I have a custom field Price
from which i want to order my results. The problem is that when i use 21.00
as input, i also get the results where the Prices are 2095.00
, 209.00
, 208.00
etc. Also, when i use (for example) 658.00
as input, i get 65.00
as result
The values of Price
are imported from an XML feed and come with the point as delimiter, so i add those to my input as well.
Here's my query
$args = array(
'posts_per_page' => 8,
'paged' => $paged,
'offset' => 0,
'post_type' => 'travel',
'land' => $_GET['slug'],
'post_status' => 'publish',
'suppress_filters' => true,
'meta_key' => 'price',
'meta_type' => 'NUMERIC',
'meta_value' => $_GET['price'],
'meta_compare' => '<',
'orderby' => 'meta_value', // I also tried meta_value_num
'order' => 'DESC'
);
$my_query = new WP_Query($args);
And i load the content with jQuery like so
var priceValue = $('<input>',{
'type' : 'number',
'id' : 'maxPriceVal',
}).on('change', function(){
$('#results').load(queryPath+'&price='+$(this).val()+'.00');
}).insertAfter('#price');
Anybody any idea what's causing the problem here?
Thanks in advance!
'meta_value_num' => $_GET['price']
AND'orderby' => 'meta_value_num'
. – hm711 Aug 4 '15 at 10:34'meta_type' => 'decimal'
? You could also try to eliminate the problem by multiplying the original value with 100 before saving and dividing by 100 when processing the value in templates. – hm711 Aug 4 '15 at 15:21$_GET
variable directly intoWP_Query
. First, this might be unsafe without sanitizing the values. But more importantly you can not be sure that the the value is of the desired type, in your casefloat
. You can check the data type byprint_r( gettype( $_GET['price'] ) )
. Usefloatval( $_GET['price'] )
to make your input a float var that WP_Query can work with... – hm711 Aug 16 '15 at 16:45