Events list short code pagination

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

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

    Hi,

    Using this shortcode can I create pagination? http://wp-event-organiser.com/documentation/shortcodes/event-list-shortcode/

    Or can I somehow duplicate the events archive page and only show past events.

    The reason I have to do this is because if I show past events, the events page will start at the first ever event and not the next event closest todays date. Maybe there is some way to fix this? πŸ˜€

    Daniel Peters
    #7159

    Hi Daniel,

    You can toggle whether or not to show pat events on the archive page via the plug-in settings (General tab), is that what you’re after? (Or maybe I’ve misunderstood πŸ™‚ )

    Stephen Harris
    #7161

    Hi,

    If I turn on show past events it starts the events list at the very first event, I need it to start at the event closest to todays date, is this possible?

    Daniel Peters
    #7162

    Ok, so I didn’t read this properly:

    Or can I somehow duplicate the events archive page and only show past events.

    Just to be sure then, you want a paginated events page showing only past events? And you wanted this is reversed,
    so that that most recently ‘past’ event is first?

    Then should do mean that events/event?myprefix=past-events displays the archive page but with only past events.
    The events/event base is will need to change depending on how you’ve configured the plug-in.

    add_action( 'pre_get_posts', 'myprefix_past_events_page', 5 );
    function myprefix_past_events_page( $query ){
    
        if( eventorganiser_is_event_query( $query ) ){
            if( $query->get( 'myprefix' ) && 'past-events' == $query->get( 'myprefix' ) ){
    
                //Show only past events.
                $query->set( 'showpastevents', true ); //This is needed for now, but maybe redundant in the future...
                $query->set( 'event_start_before', 'now' );
    
                 //Reverse the order:
                $query->set( 'order', 'DESC' );
                $query->set( 'orderby', 'eventstart' );
            }
        }
    }
    
    add_filter('query_vars', 'myprefix_register_query_vars' );
    function myprefix_register_query_vars( $qvars ){
        $qvars[] = 'myprefix';
        return $qvars;
    }
    

    This bit is entirely optional, but makes it pretty, i.e. the page can be accessed by events/event/past-events.

    add_action( 'init', 'myprefix_add_rewrite_rule', 11 );
    function myprefix_add_rewrite_rule(){
        global $wp_rewrite;
    
        //Get base regex
        $regex = str_replace( '%event%', 'past-events', $wp_rewrite->get_extra_permastruct('event') );
    
        //Get pagination base regex
        $pageregex = $wp_rewrite->pagination_base . '/?([0-9]{1,})/?$';
    
         //Add paged rewrite rule
        add_rewrite_rule( $regex.'/'.$pageregex, 'index.php?post_type=event&paged=$matches[1]&event_start_before=now&myprefix=past-events', 'top' );
    
         //Add standard rewrite urle
        add_rewrite_rule( $regex, 'index.php?post_type=event&event_start_before=now&myprefix=past-events', 'top' );
    }
    
    Stephen Harris
    #7163

    Hey thanks for this but I don’t think I was clear enough sorry.

    Currently I am using the events page without showing past events, so it starts at the next event: http://78.129.175.88/~sapphire/events/event/

    Which is fine, but then I want to show past events so I added a new page and used the events list shortcode to show past events: http://78.129.175.88/~sapphire/events/past-events/

    So my question was can I paginate the events list short code so that my past events page is paginated, which I think the code you posted above would do?

    However, I would much rather show past events on the page ‘http://78.129.175.88/~sapphire/events/event/’ which I know I can do in the events settings, but when I set this to show past events then that page will start listing events at the first ever created event, so then my question is can I change this to start listing at the next event like it currently does, but allow you to go back like the ‘← Newer events’ link does if you go to later events?

    Daniel Peters
    #7164

    Hi Daniel,

    It’s not really possible to paginate the event list shortcode due to how WordPress works.

    my question is can I change this to start listing at the next event like it currently does, but allow you to go back like the β€˜β† Newer events’ link does if you go to later events?

    … that’s an interesting question. Unfortunately that’s really quite difficult to implement, since WordPress’ pagination handling was never intended for things like that. The closest you can could easily get to is to map /events/event to map to events/event/page/X where X is the page on which the next occurring event lives

    Below is a snippet that should do that, but it comes with a health warning. You have to query the events a second time to work out the page number. There are also various assumptions made that may mean it requires tweaking before it will work without issue, but it works on my test install.

    Obviously you should have ‘show past events’ set to ‘yes’.

    add_action( 'pre_get_posts', 'myprefix_jump_to_current', 5 );
    function myprefix_jump_to_current( $query ){
    
        global $wp_query, $wp_rewrite;
    
        $paged = (int) $query->get('paged');
    
        if( eventorganiser_is_event_query( $query ) && $query->is_main_query() && ( $paged == 0 ) ){
    
            //Count how many events occur before current one:
            $events = eo_get_events( array( 'fields'=>'ids', 'event_start_before' => 'now', 'posts_per_page' => -1, ) );
    
            //Translate that to total number of pages
            $per_page = absint( get_option('posts_per_page') );
            $pages = floor( ( count( $events ) + 1 ) / $per_page );
    
                //Set the paged value accordingly....
            if( $pages != 0 && $pages != 1 ){
                $query->set('paged', $pages );
            }
        }
    }
    

    Hope that helps!

    Stephen Harris
    #7165

    Thank you I will try this tonight. πŸ˜€

    Daniel Peters
    #7166

    Hi sorry, where do I put this file? i’ve tried it in the archive-event template and my theme functions file?

    Daniel Peters
    #7167

    Ideally it should go in a ‘utility plug-in’, but it’ll work just as well as in your theme’s functions.php.

    You can use wp_die( var_dump( [some variable] ) ), e.g. wp_die(var_dump( $events) ) in the code to kill the page and inspect the passed variable to help debug.

    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.