• Resolved davideno

    (@davideno)


    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 to see the link]

Viewing 2 replies - 1 through 2 (of 2 total)
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Ordering posts by conditional sort’ is closed to new replies.