1
votes

I wanted to sort product by some items like price, date, and release year. in case of release year, I made a taxonomy=release-year&post_type=product and set year for it. now i want to sort by year. the code i have is this:


case 'rand' :
    $curr_args['orderby']  = 'rand';
    break;
case 'date' :
    $curr_args['orderby']  = 'date';
    $curr_args['order']    = $order == 'ASC' ? 'ASC' : 'DESC';
    break;
case 'price' :
    global $wpdb;
    $curr_args['orderby']  = "meta_value_num {$wpdb->posts}.ID";
    $curr_args['order']    = $order == 'DESC' ? 'DESC' : 'ASC';
    $curr_args['meta_key'] = '_price';
    break;
case 'popularity' :
    $curr_args['meta_key'] = 'total_sales';
    add_filter( 'posts_clauses', array( WC()->query, 'order_by_popularity_post_clauses' ) );
    break;
case 'release-year' :
    $curr_args['orderby'] = 'release-year';
    $curr_args['order']   = $order == 'ASC' ? 'ASC' : 'DESC';
    $curr_args['tax_query'] = array(array('taxonomy' => 'release-year', ); );   
case 'title' :
    $curr_args['orderby']  = 'title';
    $curr_args['order']    = $order == 'DESC' ? 'DESC' : 'ASC';
    break;

but it is not working. how can i change the code to possible sort by release year order?

1

1 Answers

0
votes

As you can see here - the orderby parameter has a limited set of possible values. So there is no simple way to set the orderby parameter for sorting by taxonomy name value (there is also a proposition for the Core which has been rejected and here you will find a full answer why). There are two possible solutions:

1) Create custom post meta value which will be used for sorting. I've described how to sort by meta values in the orderby parameter here

2) Try a little bit more hacky way and try to override the query via filters like here

I think that you should avoid storing this kind of data in the taxonomy - post meta is in my opinion much better place for this kind of data, but the choice is yours. Using taxonomy for it can cause future problems with the website scalability and extending.