0

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?

2 Answers 2

0

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

2
  • 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. Aug 2, 2011 at 7:25
  • You're welcome! :) Actually, I may have missed out something in the code, my bad! Aug 2, 2011 at 9:47
0

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.

1
  • 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! Aug 2, 2011 at 3:41

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.