List events for remainder of current month on archive

WordPress Event Management, Calendars & Registration Forums General Question List events for remainder of current month on archive

This topic contains 10 replies, has 2 voices, and was last updated by  Ben Harbinson 7 years, 2 months ago.

Viewing 11 posts - 1 through 11 (of 11 total)
  • Author
    Posts
  • #26105

    I’m having a lot of difficulty trying to list the remaining events for the current month on the event archive page.

    I know you can use the month archives (eg. /events/on/2017/02), however I still need to have a generic archive page (just /events/) that I can link to from anywhere in the site.

    I’m just using this line of code in archive-events.php to list the events:

    <?php eo_get_template_part( 'eo-loop-events' ); //Lists the events ?>
    

    I have tried to create various actions in functions.php

    This is far as I have got:

    add_action( 'pre_get_posts', 'alter_date_event_archives', 11 );
    function alter_date_event_archives( $query ){
    if( eventorganiser_is_event_query( $query, true ) ){
    
        $query->set('post_type', 'event');
    
        $query->set('event_start_after', 'today');
        $query->set('event_end_before', 'last day of this month');
    }
    

    }

    The problem is it displays a different number of events than if I go to the actual month archive page PLUS it overrides all the other month pages 🙁

    I ALSO have a custom loop at the top of this page to get all categories and list each event under the appropriate category. This works great until the action above which messes up this custom query.

    Any help would be appreciated.

    Ben Harbinson
    #26129

    Hi Ben,

    What you have will affect all event queries.

    Try this instead:

    add_action( 'pre_get_posts', 'alter_date_event_archives', 11 );
    function alter_date_event_archives( $query ){
    
        // Only change the main query for the events page
        if(  eo_is_event_archive() && $query->is_main_query() ){
    
            // Do not change for the year / month / day events pages
            if ( eo_is_event_archive( 'year' ) 
                || eo_is_event_archive( 'month' )
                || eo_is_event_archive( 'day' )
            ) {
                return;
            }
            $query->set('event_start_after', 'today');
            $query->set('event_end_before', 'last day of this month');
        }
    }
    
    Stephen Harris
    #26135

    Thanks Stephen, that works great.

    I had to add a function to stop the default WordPress page limit from affecting how many are displayed – this works fine.

    For some reason on any of the month archive pages (eg. /events/on/2017/02), it ends on the last day of the month fine, but for some strange reason it always starts from the beginning of the year.

    I even tested it with a completely empty functions.php file – same problem.

    Ben Harbinson
    #26142

    So, /events/on/2017/02 displays all events from 1st January 2017 up to 28th February 2017 and /events/on/2017/03 displays all events from 1st January 2017 up to 31st March 2017? Can you provide a link to the site?

    Stephen Harris
    #26143
    Ben Harbinson
    #26159

    Thanks Ben,

    I can’t see any obvious reason why that would happen. Are you able to see what the corresponding SQL query is? That might help identify the problem. You could use a plugin such as Query Monitor.

    Stephen Harris
    #26173

    Hi Stephen,

    Here is a link to the page (saved html version) with query monitor enabled, however I can’t work out from this what the issue is.<br />
    http://res189.servconfig.com/~healthab/Events_Archive_healthAbility.htm

    Looking at the Query Vars,
    – event_start_after is blank
    – event_start_before is 2017-03-31 00:00:00

    So for some reason it’s not getting the start of the month.

    I enabled a blank theme, disabled all other plugins, upgraded to the latest version of eo and the issue still happens.

    Appreciate the help.

    Ben Harbinson
    #26175

    Thanks Ben,

    Looking at the query variables, anything is correct. That prompted me to look a bit closer at the events.

    “Parkinson’s Support” runs: 2nd January 2017 – 2nd January 2018. Looking at the site’s iCal feed. I can see this a recurring event until the end of February 2017, but that each occurrence lasts one year (the initial start and end date are set with a duration of 1 year). This means that each occurrence is runs in the March 2017, and so appears in the /on/2017/03 page.

    You can probably see this on the admin calendar – they’ll be a blue block for each occurrence of that event, and it’ll stretch over 1 year.

    Stephen Harris
    #26176

    To clarify, when creating an event you are prompted for a start and end date. You can then repeat the event according to some schedule, until some point (and add/remove additional dates). The initial start and end datetimes, though, refer to the first occurrence. The duration of any additional dates, either generated by the recurrence schedule or added manually will be determined based on the start and end date.

    Stephen Harris
    #26177

    To clarify, when creating an event you are prompted for a start and end date. You can then repeat the event according to some schedule, until some point (and add/remove additional dates). The initial start and end datetimes, though, refer to the first occurrence. The duration of any additional dates, either generated by the recurrence schedule or added manually will be determined based on the start and end date.

    Stephen Harris
    #26178

    Thanks Stephen,

    It all makes sense now, the client or someone had loaded in some event dates incorrectly.
    Can’t believe I missed that.

    Thanks for you help.

    Ben Harbinson
Viewing 11 posts - 1 through 11 (of 11 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.