Title: Ordering posts by conditional sort
Last modified: May 1, 2022

---

# Ordering posts by conditional sort

 *  Resolved [davideno](https://wordpress.org/support/users/davideno/)
 * (@davideno)
 * [4 years, 1 month ago](https://wordpress.org/support/topic/ordering-posts-by-conditional-sort/)
 * I wish to sort in a special date order which brings together posts made at a 
   similar date to NOW() and in the future in all years.
 * I created and tested the MySQL to do this independently of WordPress and it works
   how I want it to.
 * `CASE WHEN DAYOFYEAR(post_date) < DAYOFYEAR(NOW()) THEN 1 ELSE 0 END ASC, DAYOFYEAR(
   post_date) ASC`
 * This does make a diffence when implanted into the following function but doesn’t
   work correctly – with or without the ‘order’ parameter.
 *     ```
       /* Order Posts Seasonally */
       function prefix_modify_query_order( $query ) {
         if ( is_main_query() ) {
   
         $query->set( 'orderby', 'CASE WHEN DAYOFYEAR(post_date) < DAYOFYEAR(NOW())-14 THEN 1 ELSE 0 END ASC, DAYOFYEAR(post_date) ASC' );
         $query->set( 'order', 'ASC' );
         }
       }
       add_action( 'pre_get_posts', 'prefix_modify_query_order' );
       ```
   
 * I am assuming that the orderby statement is too complex? Is there a way around
   this?
 * Thanks for any help.
 * David
 * The page I need help with: _[[log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fwordpress.org%2Fsupport%2Ftopic%2Fordering-posts-by-conditional-sort%2F%3Foutput_format%3Dmd&locale=en_US)
   to see the link]_

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

 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [4 years, 1 month ago](https://wordpress.org/support/topic/ordering-posts-by-conditional-sort/#post-15606997)
 * The orderby query var only accepts certain pre-defined values. You cannot pass
   arbitrary columns or SQL this way. You can use the “posts_orderby” filter to 
   get WP to use any SQL order clause you’d like.
    [https://developer.wordpress.org/reference/hooks/posts_orderby/](https://developer.wordpress.org/reference/hooks/posts_orderby/)
 * Your order clause does need to work with the SQL that WP had built. If you need
   to alter other parts of the SQL, use the “posts_request” filter where you can
   alter the entire SQL.
    [https://developer.wordpress.org/reference/hooks/posts_request/](https://developer.wordpress.org/reference/hooks/posts_request/)
 *  Thread Starter [davideno](https://wordpress.org/support/users/davideno/)
 * (@davideno)
 * [4 years, 1 month ago](https://wordpress.org/support/topic/ordering-posts-by-conditional-sort/#post-15611627)
 * Thanks very much for the help, this solved my problem completely.

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

The topic ‘Ordering posts by conditional sort’ is closed to new replies.

## Tags

 * [orderby](https://wordpress.org/support/topic-tag/orderby/)
 * [post](https://wordpress.org/support/topic-tag/post/)
 * [post_date](https://wordpress.org/support/topic-tag/post_date/)

 * In: [Developing with WordPress](https://wordpress.org/support/forum/wp-advanced/)
 * 2 replies
 * 2 participants
 * Last reply from: [davideno](https://wordpress.org/support/users/davideno/)
 * Last activity: [4 years, 1 month ago](https://wordpress.org/support/topic/ordering-posts-by-conditional-sort/#post-15611627)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
