I have an issue here that I just can't wrap my head around. Let me jump right into it:

Basically I have a site that has 4 order options;

-Name Ascending -Name Descending -Price Ascending -Price Descending

Getting it to order by "title" was easy enough after I fixed some issues. Basically just did this..

    $order = isset($_GET['order']) ? $_GET['order'] : "ASC";
    $sort_by_title = isset($_GET['sort']) ? $_GET['sort'] : "title";


    $args["orderby"] = $sort_by_title;
    $args["order"] = $order;

And finally

<a id="name-ascending" href="?order=ASC">Name Ascending</a>
<a id="name-descending" href="?order=DESC">Name Descending</a>

Now the price field is a custom field, and it's easy enough to sort by price if I replaced

$args["orderby"] = $sort_by_title;


$args["orderby"] = $custom_field_name;

It'll sort it by price and there's no issue there. The issue is figuring out how to figure out what the user clicks and determining what it should now be ordered by.

If needed, my full $args is

$args = array('post_type' => 'model-pricing', 'posts_per_page' => 5, 'meta_key' => 'Community', 'meta_value' => $community_choice, 'paged' => $paged);
    $args["orderby"] = $sort_by_title;
    $args["order"] = $order;

I'd really appreciate some help with this one guys, even some insightful thoughts on how I might tackle this issue. Thanks!

  • Could you please provide your WP_Query constructor?
    – chrisjlee
    Aug 1, 2012 at 19:47

1 Answer 1


i assume its exclusive to either sort by title $_GET['sort_title'] OR sort by custom field ( but not necessarily) and you that you pass $_GET['sort_price']. if neither are present, "title" is default.

with that in mind, check both values

 if( isset($_GET['sort_title']) ) { 
     $sort_by =  $_GET['sort_title']; 
     //orderby can accept multiple values
 elseif ( isset($_GET['sort_price']) ) { 
     $sort_by = 'meta_value';
     $args["meta_key"] = 'price';
 else { $sort_by = "title";

if so, don't forget to change your query to allow for restrictions on other custom fields.

  args['meta_query'] = array(
        'key' => 'Community',
        'value' => '$community_choice'

meaning: i'm giving top priority to "title". then if not provided, check for "price" finally, if neither are set, the default is title

  • But keeping in mind the custom field exists within the loop, I don't believe that method would accomplish what I'm looking for, unfortunately.
    – Gilipe
    Aug 2, 2012 at 13:22

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.