Using multiple sort criteria on Taxonomy Archive page

WordPress Event Management, Calendars & Registration Forums General Question Using multiple sort criteria on Taxonomy Archive page

This topic contains 3 replies, has 2 voices, and was last updated by  Tristan Tamplin 7 years, 2 months ago.

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #26190

    Hi Stephen.

    I’m attempting to make use of WordPress’ improved “orderby” to sort an event category page. For now, I’m just modifying the “taxonomy-event-category” template page rather than adding a search filter as it only pertains to a fairly specific case. This is my code:

    <?php if( has_term( 'class' , 'event-category' )):
        $args = array( 'event-category' => 'class', 'showpastevents' => 'true', 'posts_per_page' => '-1',
            'meta_query' => array(
                'class_level_clause' => array(
                    'key'     => 'class_level',
                    'compare' => 'EXISTS'
                ),
                'class_teacher_clause' => array(
                    'key'     => 'class_teacher'
                ) 
            ),
            'orderby' => array(
                'class_level_clause' => 'ASC',
                'class_teacher_clause' => 'ASC',
                'eventstart' => 'DESC'
            )
        );
        $posts = query_posts( $args );
        $event = 'class'; 
    else :
        $posts = query_posts($query_string. '&posts_per_page=-1' );
        $event = 'show';
    endif; ?>
    

    The code above works as expected, except that the last orderby parameter has no effect (e.g.; changing from DESC to ASC does not affect the order, nor does removing it entirely). Additionally, what I really want to be doing is sorting by the day of the week the event is on (including recurring events), rather than start date — so something like “eventstart(‘w’)”. But “eventstart” doesn’t behave like a meta_key, and I can’t figure out how to use it to affect the sort order.

    Any help would be greatly appreciated.

    Thanks,

    Tristan

    Tristan Tamplin
    #26203

    Hi Tristan,

    I’ll take a look at this. Eventstart isn’t a meta key, and it was implemented before WordPress supported an array for the orderby property. So it probably doesn’t work in its current implementation.

    As for sorting by weekday. This will need to be done via PHP as the API doesn’t offer a means of doing that.

    Once I’ve looked into this I’ll post back with an example snippet.

    Stephen Harris
    #26211

    Tristan,

    While this is possible to do in your case, it is not generally possible. So the best I can do is provide you a snippet to achieve what you want:

    add_filter('posts_orderby', function( $orderby, $query ){
        global $wpdb;
        $_orderby = $query->get( 'orderby' );
    
        if ( is_array( $_orderby ) && array_key_exists( 'eventstart', $_orderby ) ){
            $order_dir = $_orderby['eventstart'];
            $orderby .= ", {$wpdb->eo_events}.StartDate $order_dir, {$wpdb->eo_events}.StartTime $order_dir";
        }
    
        if ( is_array( $_orderby ) && array_key_exists( 'eventstartdayofweek', $_orderby ) ){
            $order_dir = $_orderby['eventstartdayofweek'];
            $orderby .= ", WEEKDAY({$wpdb->eo_events}.StartDate) $order_dir, {$wpdb->eo_events}.StartTime $order_dir";
        }
    
        return ltrim( $orderby, ',' );
    },10,2);
    

    Simply add that to your theme’s functions.php or a custom plugin.

    The eventstartdayofweek orderby property orders by day of week, with Monday first. If you want Sunday first then you should change the WEEKDAY function name to DAYOFWEEK.

    The eventstartdayofweek option is only available when using an array for orderby.

    This snippet will always put the eventstart or eventstartdayofweek at the end of the order clauses – regardless of where you put it in the orderby array (which is why this can’t be introduced into the core plug-in)

    Stephen Harris
    #26224

    Hi Stephen.

    I appreciate the explanation and the snippet — it hadn’t occurred to me to hook into “posts_orderby” directly. I’ve added the snippet (with the DAYOFWEEK modification) to my utility plugin and updated my query parameters, and everything is working as hoped.

    Thanks again for being so prompt and attentive!

    Tristan Tamplin
Viewing 4 posts - 1 through 4 (of 4 total)
To enable me to focus on Pro customers, only users who have a valid license for the Pro add-on may post new topics or replies in this forum. If you have a valid license, please log-in or register an account using the e-mail address you purchased the license with. If you don't you can purchase one here. Or there's always the WordPress repository forum.