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(){

Anybody any idea what's causing the problem here?

Thanks in advance!

  • Try using 'meta_value_num' => $_GET['price'] AND 'orderby' => 'meta_value_num'. – hm711 Aug 4 '15 at 10:34
  • Unfortunately that gives me the same result :( – Maurice Aug 4 '15 at 11:26
  • What about '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
  • Nope, that still gives me values of 1099, 1095 when i use 110 as input – Maurice Aug 10 '15 at 10:31
  • Okay, I just noticed that you pass the $_GET variable directly into WP_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 case float. You can check the data type by print_r( gettype( $_GET['price'] ) ). Use floatval( $_GET['price'] ) to make your input a float var that WP_Query can work with... – hm711 Aug 16 '15 at 16:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.