I have a post type gtre and metadata price.

The 'price' metadata I get using get_post_meta($post->ID, 'key', true)["price"];

So I don´t know how exactly should I write the args for WP_Query constructor, so I can get the posts in order by the 'price' meta data. I mean I want the query to ordered by 'price'.

I have tried this:

$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$temp = $wp_query; 
$wp_query = NULL;

$args = array('post_type' => 'gtre',
'meta_query' => array(
   'key'     => 'price',
$wp_query = new WP_Query($args);

But I only get the posts in 'ASC' order by time of posting.

How could be my '$args' array so I can sort or order by the 'price' meta data. And this 'price' meta data I only get if I use 'get_post_meta($post->ID, 'key', true)["price"]', I mean I can only get the price by using the function 'get_post_meta'.

Any help? Thanks.

2 Answers 2


I have achieved this in the past by using the following method.

$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;    
$args = array(
    'post_type'      => 'gtre',
    'posts_per_page' => 6,
    'paged'          => $paged,
    'meta_key'       => 'price',
    'orderby'        => 'meta_value_num',
    'order'          => 'ASC',
$pquery = new WP_Query($args);

There shouldn't be a need for the following in your code If you don't set the new query object to $wp_query.

$temp = $wp_query; 
$wp_query = NULL;

You generally don't want to alter that because it's the main query variable. By using a unique variable that isn't already being used for WordPress you can be sure this is only effecting the current query you're working on. Thus you won't have any unexpected results.

  • Hi @Brad I think I´ve found the main problem, the 'meta_key' on phpmyadmin is written '_price', so I´ve altered the string to that and I get posts. Thanks for your help :) Mar 14, 2015 at 1:37
  • 1
    Awesome glad i could help! Mar 14, 2015 at 2:06

The issue in my question is because in the wordpress database the meta key "price", is as "_price", so adding an underline in 'price' I got it working. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.