Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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

John

share|improve this question
    
Slid means? If I give you a source, will you be able to modify the solution as per your need? –  Rutwick Gangurde Jun 6 '12 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 '12 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:

www.example.com?order=ASC&orderby=meta_value_num&meta_key=price

(viewing properties in price order). Then the url

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

Would give you

www.example.com?order=ASC&orderby=meta_value_num&meta_key=bedrooms

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

E.g:

 <?php
   $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')));
 ?>

 <ul>
    <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>
 </ul>
share|improve this answer
    
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 '12 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. –  Stephen Harris Jun 12 '12 at 12:43

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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