0

I've created a filter function to filter a post type items by it's meta value _item_price. The problem is that it only works in Ascending Order and I'm not sure why. My pre_get_posts function looks like this:

function frontend_items_filter( $query ) {
    global $page_items_id

    if(!is_page($page_items_id))
        return;

    if($query->query_vars['post_type'] == 'items' && isset($_GET['filter'])){
        $sort = $_GET['item_sort'];

        // Set Ordering Parameter
        if(isset($_GET['item_sort']) && !empty($_GET['item_sort'])){
            switch($_GET['item_sort']){

                case 'price_lowest':
                    $query->set('meta_key', '_item_price');
                    $query->set('orderby', 'meta_value_num title');
                    $query->set('order', 'ASC');
                        break;

                case 'price_highest':
                    $query->set('meta_key', '_item_price');
                    $query->set('orderby', 'meta_value_num title');
                    $query->set('order', 'DESC');
                         break;

                default:
                    $query->set('orderby', 'post_date title');
                    $query->set('order', 'ASC');
            }
        }
    }
    return $query;
}
add_action( 'pre_get_posts', 'frontend_items_filter' );

Before I save my price meta I'm verifying it's is_numeric(). I tried to print out my query and run the SQL directly into PHPmyadmin, If I take the SQL below and change ASC to DESC, in my PHPmyadmin the results don't change which is peculiar.

SELECT SQL_CALC_FOUND_ROWS  wp_posts.ID FROM wp_posts
    INNER JOIN wp_term_relationships 
        ON (wp_posts.ID = wp_term_relationships.object_id) 
    INNER JOIN wp_postmeta 
        ON (wp_posts.ID = wp_postmeta.post_id) 
    WHERE 1=1  
    AND ( wp_term_relationships.term_taxonomy_id IN (10) ) 
    AND wp_posts.post_type = 'items' 
    AND (wp_posts.post_status = 'publish' 
    OR wp_posts.post_status = 'private') 
    AND (wp_postmeta.meta_key = '_item_price' ) 
    GROUP BY wp_posts.ID 
    ORDER BY wp_postmeta.meta_value+0,wp_posts.post_title ASC LIMIT 0, 10

Every time it pulls my results back in _item_price Ascending order (lowest price to highest price). What could the problem possibly be? What else can I check / verify?

Edit I've tried this with a few other fields I have and it's the same result - it will show ASC but not DESC.

3
  • How to do you access to the page which you expect to use DESC order?
    – cybmeta
    May 13, 2014 at 17:30
  • http://www.myweburl/myitempage/?filter=true&item_sort=price_highest
    – Howdy_McGee
    May 13, 2014 at 17:31
  • Maybe you have a mistyping but have you tried http://www.myweburl/myitempage/?filter=true&item_sort=price_highest?
    – cybmeta
    May 13, 2014 at 17:33

1 Answer 1

1

I'm not sure why, but it had to do with the title in my ordering, once I removed that everything started working fine. I'll leave this open for anyone who knows why.

$query->set('orderby', 'meta_value_num title'); - Does not Work

$query->set('orderby', 'meta_value_num'); - Works!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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