Order by date, then title

This topic contains 8 replies, has 2 voices, and was last updated by  Stephen Harris 10 years, 7 months ago.

Viewing 9 posts - 1 through 9 (of 9 total)
  • Author
    Posts
  • #7507

    Hi,

    I would like to get all future events and order them as follows:

    • OCT 12
    • A event
    • B event
    • X event
    • OCT 13
    • D event
    • F event

    The code looks like this

     $events = eo_get_events(array(
         'get_posts'=>100,
         'event_start_after'=>'today',
         'showpastevents'=>false,//Will be deprecated, but set it to true to play it safe.
    'orderby' => 'date title',
    'order'=>'ASC'       
    ));
    

    I know the doc here (http://codex.wp-event-organiser.com/function-eo_get_events.html) says orderby only takes eventstart and eventend, however when I order by “title” alone it works.

    Has anyone managed to do that?
    Would greatly appreciate help!

    Anthony Wong
    #7508

    Hi Anthony,

    Actually the orderby argument can take anything WP_Query or get_posts() can. The plug-in adds support for eventstart and eventend.

    Unfortunately WordPress doesn’t handle multiple sort criteria, so you need to implement yourself. The below does that, and adds support for eventstartalpha as value for orderby. This sorts by date (no time), and then sorts by title. So the events are then returned in the desired order, but you’ll still need to edit the templates to group events by date.

    add_filter( 'posts_orderby', 'anthony_sort_events_eventstartalpha', 20, 2 );
    function anthony_sort_events_eventstartalpha( $order, $query ){
    
        $order_dir = strtoupper( $query->get( 'order') );
        $order_dir = ( $order_dir === 'DESC' ? 'DESC' : 'ASC' );
    
        if( eventorganiser_is_event_query( $query ) && 'eventstartalpha' == $query->get( 'orderby') ){
            $order = " {$wpdb->eo_events}.StartDate $order_dir, {$wpdb->posts}.post_title $order_dir";
        }
        return $order;
    }
    

    Example usage:

       [eo_events orderby="eventstartalpha" order="ASC" event_start_after="today"]
    
    Stephen Harris
    #7515

    Thanks Stephen

    Where should I use this code?
    in events-organiser-pro/includes/functions.php or in the file I’m editing:
    events-organiser-pro/includes/shortcodes.php

    So far I’ve pasted the
    function anthony_sort_events_eventstartalpha( $order, $query ){
    function in functions.php

    and add_filter in the shortcodes.php file like this

        ...
    $query = wp_parse_args(
            $input,
            array(
                    'post_type' => 'event',
                    'paged' => max( 1, get_query_var( 'paged' ) ),
                    'suppress_filters' => false,
                    'posts_per_page' => 10,
            )
    );
    
    add_filter( 'posts_orderby', 'anthony_sort_events_eventstartalpha', 20, 2 );
    
    if( empty( $query['venue_query'][0]['value'] ) ){
        unset( $query['venue_query'][0]);
    }
    if( empty( $query['venue_query'][1]['value'] ) ){
        unset( $query['venue_query'][1]);
    }
    
    global $eo_event_loop;
    $eo_event_loop = new WP_Query( $query );
    
    
    ob_start();
    $template_file = eo_locate_template( array( 'search-event-list.php', 'event-list.php' ), true, false );
    $html .= ob_get_contents();
    ob_end_clean();
    
    return $html;
    

    Finally my shortcode looks lie:

    [event_search filters="event_category,event_venue,date" orderby="eventstartalpha" order="ASC" event_start_after="today"]
    

    However it’s not working for me…

    Anthony Wong
    #7526

    Hi Anythony,

    The code should exist in a separate plug-in, or your theme’s functions.php.

    However, I see you require this for the event search shortcode. There’s a limitation in that unlike [eo_events] you cannot alter a lot of the query settings (I’ll be fixing this shortly, see related topic: http://wp-event-organiser.com/forums/topic/eliminate-pagination/#post-7525).

    For now the only change you need to make to the core-plugin files is the one below. Note that you should not normally edit the plug-in files as changes are lost when you update. (Please see: http://wp-event-organiser.com/blog/tutorial/where-should-i-put-code-from-the-tutorials/)

    $query = wp_parse_args(
            $input,
            array(
                    'post_type' => 'event',
                    'orderby' => 'eventstartalpha',
                    'order' => 'ASC',
                    'paged' => max( 1, get_query_var( 'paged' ) ),
                    'suppress_filters' => false,
                    'posts_per_page' => 10,
            )
    );
    

    to replace the existing $query (in the above, which should be near line 268 in includes/shortcodes.php).

    From 1.4.1+ you’ll be able to do the following

      [event_search orderby="eventstartalpha"]
    

    (assuming you’ve included the code from my first post in a plug-in or functions.php to add support for eventstartalpha)

    Stephen Harris
    #7551

    Thank you so much for your help Stephen.

    It’s not working for me though. I get a blank results page whenever I uncomment ‘orderby’ => ‘eventstartalpha’

    To summerize, my theme’s functions.php file has

        add_filter( 'posts_orderby', 'anthony_sort_events_eventstartalpha', 20, 2 );
    function anthony_sort_events_eventstartalpha( $order, $query ){
    
        $order_dir = strtoupper( $query->get( 'order') );
        $order_dir = ( $order_dir === 'DESC' ? 'DESC' : 'ASC' );
    
        if( eventorganiser_is_event_query( $query ) && 'eventstartalpha' == $query->get( 'orderby') ){
            $order = " {$wpdb->eo_events}.StartDate $order_dir, {$wpdb->posts}.post_title $order_dir";
        }
        return $order;
    }
    

    while my shortcode.php file has

    $query = wp_parse_args(
            $input,
            array(
                    'post_type' => 'event',
                    'orderby' => 'eventstartalpha',
                    'order' => 'ASC',
                    'paged' => max( 1, get_query_var( 'paged' ) ),
                    'suppress_filters' => false,
                    'posts_per_page' => 1000,
            )
        );
    

    I’ve tried moving the addfilter line to the shortcodes.php file but no change.

    add_filter( 'posts_orderby', 'anthony_sort_events_eventstartalpha', 20, 2 );
    
    Anthony Wong
    #7552

    Sorry Anthony,

    I missed out the line global $wpdb; – which should in anthony_sort_events_eventstartalpha() above $order_dir.

    Stephen Harris
    #7559

    Awesome, thanks you!

    Anthony Wong
    #7575

    May I ask what if I wanted to order by venue name too (or instead)?
    Is something like:

    $order = " {$wpdb->eo_events}.StartDate $order_dir, {$wpdb->eo_venue}.name $order_dir";

    possible?

    Anthony Wong
    #7582

    Unfortunately note, you can’t order by venue name. (Though technically you probably could its a lot more involved, you would have to ensure the venue table is joined).

    Stephen Harris
Viewing 9 posts - 1 through 9 (of 9 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.