I've got a site with a couple thousand time-stamped posts in a custom post type. These get displayed by a few taxonomies and a date query in various views (I've got that all working just fine already). The tricky part is ordering them by a unique date format: the client insists that posts should be ordered DESC by day, then ASC by time within each day. As far as I can see in the Codex, order only takes date as a param, not day & time as distinguishable values. Is there any way to accomplish what I'm looking for without substantial re-writes of a half-dozen archive-tax template files?

I'm using the following to sort DESC by date already and would love a solution that fits into this function:

function my_post_sort( $vars ) {
    if ( is_tax(array('series', 'speakers', 'topics', 'venues'))) {
        $vars['order'] = 'ASC';
    return $vars;
add_filter('query_vars', 'my_post_sort');

1 Answer 1


You're not using query_vars quite right - this filter is for whitelisting "public" query vars, not actually setting their values. It's also called rather early (wp::parse_request), so any conditional tags like is_tax() will always be false at this stage.

Use the pre_get_posts action for overriding the default query. Having said that, you need a custom ORDER BY clause. Use the posts_orderby filter:

function wpse_179686_posts_orderby( $orderby, $wp_query ) {
    if ( $wp_query->is_main_query() && $wp_query->is_tax( array( 'series', 'speakers', 'topics', 'venues' ) ) )
        $orderby = 'DATE( post_date ) DESC, TIME( post_date ) ASC';

    return $orderby;

add_filter( 'posts_orderby', 'wpse_179686_posts_orderby', 10, 2 );
  • Great, that's exactly what I needed: didn't know about the DATE() and TIME() functions. And thanks for the reminder about which filter to use: not quite sure how I ended up with query_vars, but posts_orderby was what I meant. Commented Mar 4, 2015 at 0:22
  • Note that posts_orderby is for directly altering the SQL query. If you just need to change the default sorting, stick to pre_get_posts and $wp_query->set( 'orderby', 'title_or_whatever' ); Happy coding! Commented Mar 4, 2015 at 11:55

Your Answer

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

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