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'm using scribu's Query Multiple Taxonomies to filter posts through multiple taxonomies (which works great!).

However, I'd like to allow people to perform further sorting on the results based on custom fields which have numeric values (eg. rate_per__private_session or rate_per_group) by clicking on a link.

I thought that something like the following would be all there was to it but clearly it is not.

<a href="<?php query_posts($query_string . '&meta_key=rate_per_private_session'); ?>">Sort by Rate per Private Session</a> | 
<a href="<?php query_posts($query_string . '&meta_key=rate_per_group'); ?>">Sort by Rate per Group</a>

How do I do this?

share|improve this question
add comment

2 Answers

You could use a javascript redirect to for passing the appropriate query_params. Checkout WP's default widgets file for the categories dropdown widget. When you select a new category, it automatically refreshes the page and redirects you to the archive for that category. I used this same trick (or code for that matter!) to change the query_posts for sorting using GDStar's query params.

Try this:

    <a href='#' id='sort_by_rate_private' value>Sort by Rate per Private Session</a>
    <a href='#' id='sort_by_rate_group'>Sort by Rate per Group</a>

<script type='text/javascript'>
jQuery('#sort_by_rate_private').click(function(){
     location.href = "<?php echo home_url(); ?>/?sortby=rate_per_private_session";
} 
jQuery('sort_by_rate_group').click(function(){
     location.href = "<?php echo home_url(); ?>/?sortby=rate_per_group";
} 
</script>

Now get the $_POST['sortby'] value, and check it as follows:

<?php
    if(isset($_POST['sortby'] && $_POST['sortby']=='rate_per_private_session')
    {
      query_posts($query_string . '&meta_key=rate_per_private_session');
    }
    else if(isset($_POST['sortby'] && $_POST['sortby']=='rate_per_group')
    {
      query_posts($query_string . '&meta_key=rate_per_group');
    }
    else
    {
      //nothing is set, hence default
      //whatever
    } 
?>

Just make sure your url is right. Also check the jQuery(I have written as far as I could remember). Try this and let me know!

Thanks, Rutwick

share|improve this answer
    
Hi Rutwick. I couldn't get your solution to work but thanks for answering. After much digging, I finally stumbled upon the WP function I was looking for. –  annabwashere Aug 2 '11 at 7:25
    
You're welcome! :) Actually, I may have missed out something in the code, my bad! –  Rutwick Gangurde Aug 2 '11 at 9:47
add comment
up vote 0 down vote accepted

I think I got it on my own. The code should be:

<a href="<?php echo add_query_arg( 'order_by', 'rate_private' ); ?>">Sort by private</a>
<a href="<?php echo add_query_arg( 'order_by', 'rate_company' ); ?>">Sort by company</a>

Note: I used another plugin, Custom Query Fields, which has "order_by" query var made available.

share|improve this answer
    
Glad you found a solution! I guess that's what my code does, just that I'm doing it without using a plugin and query_args! –  Rutwick Gangurde Aug 2 '11 at 3:41
add comment

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.