Events to Appear in Random Order

Home Forums Calendar Products Events Calendar PRO Events to Appear in Random Order

Viewing 15 posts - 1 through 15 (of 31 total)
  • Author
    Posts
  • #1602740
    titusraj
    Participant

    Is there a method to show the events in random order on the front page , so the events listed for long periods don’t sit on the front page all the time.

    This is my website http://www.eventsforlondon.co.uk

    #1604364
    Sky
    Keymaster

    Hi there,

    I’d be happy to help with your question.

    It looks like this has been asked before. Here is the solution that someone else came up with:

    https://pastebin.com/AhyMajCK

    Can you try adding that to your functions.php file and see if it works for you? Some of the logic may need to be tweaked a bit, but I think this may be exactly what you need.

    Thanks,
    Sky

    #1604636
    titusraj
    Participant

    Hi Sky,

    Thanks for this.

    Problem with this code is, the events that start before today, don’t show at all in photo view which is my front page.

    Titus

    #1605226
    Sky
    Keymaster

    Hi again,

    Looking at the example I linked to, I now see that this was meant for someone who was including the events into their main blog feed.

    To clarify, you are saying that when using that code, past events no longer show up?

    Thanks,
    Sky

    #1605359
    titusraj
    Participant

    Hi Sky,

    Not past events but events that are current, but started in the past from
    today. Basically events that started before today but are still going on,
    those disappear.

    #1606571
    titusraj
    Participant

    Example, an event that started in August 25th but goes on to August 30th don’t show on any page with a photo view.
    Events that start on Ausgust 27th (Today) and August 28th (tomorrow and beyond ) show up.

    #1606595
    Sky
    Keymaster

    Hi again,

    Thanks for the additional information. I can see now why that would happen. It’s looking for any event that whose start date is after the current date. Maybe instead of querying by _EventStartDate you could use _EventEndDate?

    Let me know if that works for you.

    Thanks,
    Sky

    #1624233
    titusraj
    Participant

    Unfortunately EventEndDate? did not work at all.

    #1624734
    Sky
    Keymaster

    Hi again,

    Looking more at that snippet, you will need to change the conditional.

    I would change if ( $query->is_home() && $query->is_main_query() ) to if ( is_front_page() )

    The original was meant to target when events were included in the main blog feed.

    Hope that helps.

    Thanks,
    Sky

    #1625998
    titusraj
    Participant

    Hi Sky,

    I have tried, there are no changes. The code only works in it’s original form.

    Would you like my username and password to try?

    Titus

    #1626478
    Sky
    Keymaster

    Hi again,

    Unfortunately, we cannot login to customer sites to make changes.

    I do however, think I have a working solution for you. Another customer asked a similar question yesterday and I came up with something that I think is what you need.

    Instead of sorting the events randomly, you can sort by date and put the newest events at the top and long running events on the bottom.

    Try this snippet that I created:

    https://gist.github.com/skyshab/398dd54a41389c39b682a3c32c5dc716

    One of the other problems you may have been having with the code is that the photo view seems to have a higher priority for the filter. I had to bump that up to 500 in my code for it to take effect.

    Let me know if that works for you.

    Thanks,
    Sky

    #1629702
    titusraj
    Participant

    Unfortunately that did not work at all, in any case I have left the code there for you to have a look.

    #1630091
    Sky
    Keymaster

    Hi again,

    I just re-tested that code, and it is working on my local install. I did notice that the results may not be ideal when there are many events, as the farthest in the future are on top. I’m not really sure what the best solution is for this, but I was able to randomize the events on my front page with this code:


    /*
    * Randomize events on front page.
    */
    add_action( 'pre_get_posts', 'push_older_events_to_bottom', 500 );
    function push_older_events_to_bottom( $query ) {
    if ( is_front_page() ) {
    $query->set( 'orderby', 'rand' );
    }
    return $query;
    }

    If that is not working for you, can you try temporarily switching to the Twenty Seventeen theme to see if it works with that? It’s possible that your theme may be doing something with the query that overrides what we’re trying here.

    Thanks,
    Sky

    #1631753
    titusraj
    Participant

    Hi Sky.

    I deactivated all plugins
    https://snag.gy/tvOK1N.jpg

    Used twenty seventeen theme

    Added the code on themes functions
    https://snag.gy/BgMESw.jpg

    Purge the Cache

    Still same result

    https://snag.gy/FJyLG7.jpg

    #1632150
    Sky
    Keymaster

    Hi again,

    OK, I think I have it figured out now.

    The “is_front_page()” conditional only works if you have a static page set for the home page. When the Main Events page is set for the home page, we need to use “tribe_is_events_home()” instead.

    I’m assuming this must be your setup, since the snippet was not working for you. Here is an updated version that should do the job in your case.


    /*
    * Reorder events on front page main calendar to be random.
    */
    add_action( 'pre_get_posts', 'my_randomize_posts', 500 );
    function my_randomize_posts( $query ) {

    if ( tribe_is_events_front_page() ) {
    $query->set( 'orderby', 'rand' );
    }

    return $query;
    }

    Let me know if that works for you.

    And thanks for your patience on this.

    Sky

Viewing 15 posts - 1 through 15 (of 31 total)
  • The topic ‘Events to Appear in Random Order’ is closed to new replies.