Help Desk

Events to Appear in Random Order

  • Posts: 104 Topics: 43
    | Permalink

    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

    Posts: 2356
    | Permalink

    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

    Posts: 104 Topics: 43
    | Permalink

    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

    Posts: 2356
    | Permalink

    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

    Posts: 104 Topics: 43
    | Permalink

    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.

    Posts: 104 Topics: 43
    | Permalink

    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.

    Posts: 2356
    | Permalink

    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

    Posts: 104 Topics: 43
    | Permalink

    Unfortunately EventEndDate? did not work at all.

    Posts: 2356
    | Permalink

    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

    Posts: 104 Topics: 43
    | Permalink

    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

    Posts: 2356
    | Permalink

    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:

    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

    Posts: 104 Topics: 43
    | Permalink

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

    Posts: 2356
    | Permalink

    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

    Posts: 104 Topics: 43
    | Permalink

    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

    Posts: 2356
    | Permalink

    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

    Posts: 104 Topics: 43
    | Permalink

    HI Sky,

    This is how it is set up

    https://snag.gy/oatqiQ.jpg

    This is the code added

    https://snag.gy/ySGcB5.jpg

    Unfortunately no result.

    https://snag.gy/wKdYgC.jpg

    Titus

    Posts: 2356
    | Permalink

    Hi again,

    I have tested both of these snippets with the Twenty Seventeen theme. Seeing that you have “main events page” selected for your home page, the second one should work for you.

    I would try once again switching to Twenty Seventeen, disable ALL other plugins, flush any caches and look again.

    Thanks,
    Sky

    Posts: 104 Topics: 43
    | Permalink

    Hi Sky,

    Do you meant this code?

    Titus

    <?php
    /*
    * Reorder events so that the ones that are running over a long period
    * show up at the bottom of the list in a given view
    */
    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’, ‘meta_value’ );
    $query->set( ‘meta_key’, ‘_EventStartDate’ );
    $query->set( ‘order’, ‘DESC’ );
    }
    return $query;
    }

    Posts: 2356
    | Permalink

    Hi again,

    No. The latest snippet that is working for me is:

    /*
    * 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;
    }
    

    Thanks,
    Sky

    Posts: 104 Topics: 43
    | Permalink

    Hi Sky,

    Unfortunately that did not work.

    Lets forget random.

    What about latest post on front page photo view?

    Can this be done, I think it will be very useful to keep the site looking good with fresh content every time someone visits.

    Maybe you can check with the guys who have worked with the plugin for a longer period.

    Titus

    Posts: 2356
    | Permalink

    Hi there,

    Hooking into the “pre_get_posts” filter is the way to change the query. This is all standard WordPress stuff. See the Codex entry for more information about working with the query object: https://codex.wordpress.org/Plugin_API/Action_Reference/pre_get_posts

    The only thing specific to events here is the conditional, tribe_is_events_front_page(). This will return true when “Main Events Page” is set to be the homepage in WordPress > Settings > Reading.

    Can you confirm for me that this is how you have your site set up?

    The code I’ve been providing works on my end. If it is not working for you, either you are not pasting it in the correct place, or you are using different settings than what I am assuming when testing.

    Can you try just replacing the conditional with “true” and see if that makes any difference?

    Thanks,
    Sky

    Posts: 104 Topics: 43
    | Permalink

    Hi Sky,

    Site is set up with Main Events Page as Home Page.

    I am sure I have been pasting in the correct place.

    Which word do I replace with “true”

    Titus

    Posts: 2356
    | Permalink

    Hi Titus,

    You can just remove the conditional altogether. I just wanted to make sure that’s not where the issue was for you. But if you have “Main Events Page” set for “Home Page”, that’s probably not it.

    Here are some screenshots showing the code I provided in my functions.php file, and then how it resolves on the front end of my site. You can see that each time I load the page, the events are in a different order.

    I have my homepage set to “Main Events Page” and the default view set to “photo” just as you do.

    I provide variations on this code to customers all the time, and am not aware of anything aside from a theme/plugin overriding the query that would prevent it from working if added to your site correctly.

    My suggestion is to try switching to Twenty Seventeen and disable all plugins, flush any caches, and start from there. If you can’t get the code to work in that configuration, you may need to contact your host or have a WordPress developer take a look at what you’re trying to do.

    Thanks,
    Sky

    Posts: 104 Topics: 43
    | Permalink

    I am only using events calendar plugins ! Unless there is a conflict within your plugins.
    Host is siteground, which is very good.
    Have purged the cache.
    Still does not work.
    Will request again in a few months to see if there is any other solution.
    Titus

    Posts: 2356
    | Permalink

    Hi again,

    Are you also switching to the Twenty Seventeen theme to test? That’s the most likely culprit.

    Thanks,
    Sky

    Posts: 104 Topics: 43
    | Permalink

    Strange , I have tried with twenty seventeen, have you tried this on a live environment on a different server?

    Posts: 2356
    | Permalink

    Hi again,

    You can try creating a quick WordPress instance on http://poopy.life

    If you are able to recreate the issue there, let me know and I’ll take a look at what you’re doing.

    Thanks,
    Sky

    Posts: 104 Topics: 43
    | Permalink

    can you please refer to another support engineer and see if they have a different code that might work.

    Basically need to make the front page random, or ideally the latest additions. I am stuck with the same photo front page now and if it is a long event, it just stays on the front page.

    Posts: 2356
    | Permalink

    Hi again,

    Please note that we are limited in the amount of support we can provide for customizations such as this. We’re happy to help when it’s something we can quickly refer you to a snippet or documentation. If you’re not able to get the snippets we provide working, you’ll need to find a professional developer who can assist you.

    We share snippets amongst each other, and the code I’ve provided is using basic WordPress functionality to modify the query. The only thing specific to our plugins in there is the conditional, which I recommended temporarily removing to troubleshoot. If it still doesn’t work for you without the conditional, I would suggest visiting the WordPress Codex or Developer’s Resources site to see how the hook and query object works, and you can modify it as needed.

    I’m sorry I wasn’t able to help to your satisfaction, but I wish you luck getting it figured out.

    Thanks,
    Sky

    Posts: 104 Topics: 43
    | Permalink
    This reply has been marked as private.

The topic ‘Events to Appear in Random Order’ is closed to new replies.