I have a custom post type "Properties".

These properties posts are being listed out on page "Properties For Let".

Each property post has various custom meta including price, and number of bedrooms.

The properties will initially be listed in the order defined by the original wp query I set.

However at the top of the page I wish to have links that upon click will sort the posts by Price, Date Added and Number Of Bedrooms.

What is the best practice for doing this? I have seen people advise on addin query strings to URLs etc but would like some solid advice

Thanks in advance for any help


  • Slid means? If I give you a source, will you be able to modify the solution as per your need? Jun 6, 2012 at 12:12
  • 2
    At what point did I ask for source code, couldnt be more from it .... I asked for advice and best practice, surely that is what this forum is for??? Should I use query strings appended to URLs, should I use a Get function, what is the best way to achieve this, I hardly call this asking for source code and think is a valid question as I do not know the best way to achieve this.
    – John
    Jun 6, 2012 at 12:21

2 Answers 2


The function add_query_arg adds or replaces 'GET' parameters in a given (or the current) url.

For instance, suppose someone is viewing the page:


(viewing properties in price order). Then the url

$url = add_query_arg(array('meta_key'=>'bedrooms','orderby'=>'meta_value_num'));

Would give you


(the url to order properties by number of bedrooms).


   $by_rooms= esc_url(add_query_arg(array('meta_key'=>'bedrooms','orderby'=>'meta_value_num')));
   $by_price = esc_url(add_query_arg(array('meta_key'=>'price','orderby'=>'meta_value_num')));
   $by_data = esc_url(add_query_arg(array('meta_key'=>false,'orderby'=>'date')));

    <li> <a href="<?php echo $by_price;?>">Order by price</a></li>
    <li> <a href="<?php echo $by_rooms;?>">Order by rooms</a></li>
    <li> <a href="<?php echo $by_date;?>">Order by date</a></li>
  • This is adding the query string correctly to the URL however the posts aren't sorting. Does it make any difference that this is being actioned on a 'Page' as opposed to say an archive? Don't understand why it's not working
    – John
    Jun 12, 2012 at 12:37
  • Yes, if you are using a WP_Query object then you need to use get_query_var('orderby') etc to retrieve each of the parameters ad use them in that query. Jun 12, 2012 at 12:43

I have explained a method over here. You'll need to tweak the code a little bit.

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.