Home › Forums › Calendar Products › Events Calendar PRO › Events to Appear in Random Order
- This topic has 30 replies, 2 voices, and was last updated 5 years, 5 months ago by Sky.
-
AuthorPosts
-
August 22, 2018 at 12:11 am #1602740titusrajParticipant
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
August 23, 2018 at 11:55 am #1604364SkyKeymasterHi 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:
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,
SkyAugust 23, 2018 at 6:33 pm #1604636titusrajParticipantHi 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
August 24, 2018 at 9:57 am #1605226SkyKeymasterHi 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,
SkyAugust 24, 2018 at 12:00 pm #1605359titusrajParticipantHi 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.August 27, 2018 at 7:40 am #1606571titusrajParticipantExample, 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.August 27, 2018 at 7:59 am #1606595SkyKeymasterHi 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,
SkySeptember 18, 2018 at 7:55 am #1624233titusrajParticipantUnfortunately EventEndDate? did not work at all.
September 18, 2018 at 12:33 pm #1624734SkyKeymasterHi again,
Looking more at that snippet, you will need to change the conditional.
I would change
if ( $query->is_home() && $query->is_main_query() )
toif ( is_front_page() )
The original was meant to target when events were included in the main blog feed.
Hope that helps.
Thanks,
SkySeptember 19, 2018 at 7:14 pm #1625998titusrajParticipantHi 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
September 20, 2018 at 8:48 am #1626478SkyKeymasterHi 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,
SkySeptember 24, 2018 at 8:51 pm #1629702titusrajParticipantUnfortunately that did not work at all, in any case I have left the code there for you to have a look.
September 25, 2018 at 8:49 am #1630091SkyKeymasterHi 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,
SkySeptember 26, 2018 at 7:49 pm #1631753titusrajParticipantHi Sky.
I deactivated all plugins
https://snag.gy/tvOK1N.jpgUsed twenty seventeen theme
Added the code on themes functions
https://snag.gy/BgMESw.jpgPurge the Cache
Still same result
September 27, 2018 at 8:10 am #1632150SkyKeymasterHi 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
-
AuthorPosts
- The topic ‘Events to Appear in Random Order’ is closed to new replies.