Show event of current day first

This topic contains 3 replies, has 2 voices, and was last updated by  Stephen Harris 9 years ago.

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

    Hello,

    How to show event of current day first on my calendar when I click on a day? Now it shows events that are still going (example art exhibitions).

    Thanks!

    Jukka Varis
    #21354

    Hi Jukka,

    Is the “Events on < day >” page? That will show all events running on that particular day. It’s possible with a few lines of code to show only events that start on that day (if tha is what you want).

    Or in the Settings > Event Organiser > General you can opt to show only future events and to consider a currently running event as past. That will mean those long-running events will not appear after they have started.

    Stephen Harris
    #21654

    Yes, I’d like to know that code to do the trick in my question. I’d like to have those long running events on day page but at the bottom.

    Jukka Varis
    #21688

    This snippet will ensure that an event will only appear on the day page if it starts on that day (rather than is running at any point on that day as it currently is):

    add_action( 'pre_get_posts', function( $query ) {
    
        //If querying for all events starting on given date, set the date parameters
        if ( $query->get( 'ondate' ) ) {
    
            //Normalise date delimiter
            $ondate_start = str_replace( '/', '-', $query->get( 'ondate' ) );
            $ondate_end = str_replace( '/', '-', $query->get( 'ondate' ) );
    
            $parts = count(explode('-',$ondate_start));
            if( $parts == 1 && is_numeric($ondate_start) ){
                //Numeric - interpret as year
                $ondate_start .= '-01-01';
                $ondate_end .= '-12-31';
            }elseif( $parts == 2 ){
                // 2012-01 format: interpret as month
                $ondate_start .= '-01';
                try{
                    $end = new DateTime($ondate_start);
                    $ondate_end = $end->format('Y-m-t');
                }catch( Exception $e){
                    $query->set('ondate',false);
                }
            }
    
            $query->set( 'post_type', 'event' );
            $query->set( 'event_start_before', $ondate_end );
            $query->set( 'event_start_after', $ondate_start );
    
        }
    
    
    }, 12, 1 );
    

    If you wanted events running that day which started previously you would need to do a separate query for that.

    Stephen Harris
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.