Support » Plugin: Event Organiser » How to change option of past events’ visibility from front-end?

  • Hi!

    I would like end-user have the possibility to decide whether to show or not past events. I imagine there is a button or checkbox on a website which changes global variable “showpastevents” (and probably query too). I suppose klicking this button should call a php function and refresh a page. Of course only for a session. Next time website starts normaly anew with default settings from database. Absolutely the best would be write this to cookies and read on start.

    How it could be solved?
    Thanks for all suggestion


Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Author Stephen Harris


    I think you’ve described the solution :).

    There is no function to change the setting – settings are stored as a serialised array in the site options under the name eventorganiser_options, so you can use WordPres’ API to do this: but that will impact all users!

    The best thing to do is something like:

        add_filter( 'pre_get_posts', function( $query ) {
             if ( $query->is_main_query() && ! is_admin() && eventorganiser_is_event_query( $query ) ) {
                 if ( /* option has been set */ ) {
                     $query->set( 'showpastevents', false ); 
        } );

    But note: this will not impact the event list shortcodes/widgets only the ‘main’ query ($query->is_main_query()) and it will not work for the calendars as they are populated via AJAX (! is_admin()).

    You could remove the $query->is_main_query() if you really wanted to, but I would advise you not to remove ! is_admin() as it will impact the event admin pages.

    Every (post) query for the front and back end are passed through that filter, so you need to make sure that it is a query you actually want to modify. Being too ‘loose’ here will cause unintended consequences.

    Thank you, Stephen. I will test it.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘How to change option of past events’ visibility from front-end?’ is closed to new replies.