Title: Orderby nesting
Last modified: August 19, 2016

---

# Orderby nesting

 *  [canon2k9](https://wordpress.org/support/users/canon2k9/)
 * (@canon2k9)
 * [16 years, 5 months ago](https://wordpress.org/support/topic/orderby-nesting/)
 * Here is the situation. I have posts that I would like sorted in their respective
   categories. I can get the posts to sort once using the orderby statement, however,
   need to sort even further.
 * For example. year, month, and selection number.
 *     ```
       add_action('pre_get_posts','sort_categories_by_title');
   
       function sort_categories_by_title($x) {
       	if(is_category()) {
       		$x->query_vars['orderby'] = 'meta_value';
       		$x->query_vars['meta_key'] = 'selection-no';
       		$x->query_vars['meta_key'] = 'release-month';
       		$x->query_vars['meta_key'] = 'release-year';
       		$x->query_vars['order'] = 'asc';
       	}
       }
       ```
   
 * The result is a single sort by the last item in the list, release-year. I have
   tried all I could but with no success. Any help would be appreciated.
 * Incidentally, this code is adapted from a plugin available [http://www.mikesmullin.com](http://www.mikesmullin.com).

Viewing 2 replies - 1 through 2 (of 2 total)

 *  Thread Starter [canon2k9](https://wordpress.org/support/users/canon2k9/)
 * (@canon2k9)
 * [16 years, 5 months ago](https://wordpress.org/support/topic/orderby-nesting/#post-1324574)
 * Any help would be appreciated. I have read of SQL quieries with multiple ORDER
   BY statements, but am unable to edit one to work with WordPress. Further, I am
   under an impression that an array may work, though cannot forge one to work either.
   Again, any help would be appreciated.
 *  [Decio](https://wordpress.org/support/users/decio/)
 * (@decio)
 * [15 years, 11 months ago](https://wordpress.org/support/topic/orderby-nesting/#post-1324805)
 * Have you found an answer? I´m having a similiar problem.

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Orderby nesting’ is closed to new replies.

## Tags

 * [categories](https://wordpress.org/support/topic-tag/categories/)
 * [category](https://wordpress.org/support/topic-tag/category/)
 * [functions](https://wordpress.org/support/topic-tag/functions/)
 * [is_category](https://wordpress.org/support/topic-tag/is_category/)
 * [meta_key](https://wordpress.org/support/topic-tag/meta_key/)
 * [meta_value)](https://wordpress.org/support/topic-tag/meta_value/)
 * [orderby](https://wordpress.org/support/topic-tag/orderby/)
 * [pre_get_posts](https://wordpress.org/support/topic-tag/pre_get_posts/)
 * [query](https://wordpress.org/support/topic-tag/query/)
 * [sort](https://wordpress.org/support/topic-tag/sort/)

 * 2 replies
 * 2 participants
 * Last reply from: [Decio](https://wordpress.org/support/users/decio/)
 * Last activity: [15 years, 11 months ago](https://wordpress.org/support/topic/orderby-nesting/#post-1324805)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
