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 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..

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

Then

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

With

$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!

share|improve this question
    
Could you please provide your WP_Query constructor? –  chrisjlee Aug 1 '12 at 19:47
add comment

1 Answer

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(
    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

share|improve this answer
    
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 '12 at 13:22
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.