Home › Forums › Calendar Products › Events Calendar PRO › Random Featured event – Custom Widget
- This topic has 14 replies, 5 voices, and was last updated 10 years, 9 months ago by
Support Droid.
-
AuthorPosts
-
February 16, 2012 at 7:31 am #15257
Paul
ParticipantIm trying to display a random event from the featured event category on a site ive built. Ive followed the custom widget tutorial and have this code
$posts = tribe_get_events(array( 'eventDisplay'=>'upcoming','numResults'=>1,'orderby'=>'rand','tribe_events_cat'=>'featured' ));which only works if i remove the eventDisplay part.
Is there a way to display 1 random upcoming event from a specific category?
CheersFebruary 16, 2012 at 12:41 pm #15283Rob
MemberHey Paul. Interesting question; let me see if Jonah has any ideas when he hits the forum this afternoon.
February 16, 2012 at 1:56 pm #15298Jonah
ParticipantHey Paul,
I don’t think you can use the ‘tribe_events_cat’ as an argument. Try tax_query instead: http://codex.wordpress.org/Class_Reference/WP_Query#Taxonomy_Parameters
You can also just use a regular WP_Query instead of tribe_get_events too.
February 17, 2012 at 3:11 am #15310Paul
ParticipantHey Jonah,
Thanks for the reply. Im struggling to get this working. Can you give me some pointers.This is what i have, but isnt working
if( function_exists( 'tribe_get_events' ) ) {
$posts = tribe_get_events(array(
'eventDisplay'=>'upcoming',
'numResults'=>1,
'orderby'=>'rand',
'tax_query'=> array(
'taxonomy' => 'tribe_events_cat',
'field' => 'slug',
'terms'=> 'featured',
)));$template = TribeEventsTemplates::getTemplateHierarchy('widget-featured-display');
}February 17, 2012 at 3:13 am #15311Paul
ParticipantIm unsure how to replace tribe_get_events with WP_Query correctly.
February 17, 2012 at 11:00 am #15332Jonah
ParticipantHey Paul, you simply need to execute a WP_Query – you can find an example of usage here: http://codex.wordpress.org/Class_Reference/WP_Query#Usage
You’ll want to pass in tribe_events for the post_type and your category in a tax_query.
February 17, 2012 at 1:16 pm #15339Paul
ParticipantHi Jonah,
Ive gone down the WP_Query route as suggested, but still finding the same issue. I can get a random event from the featured category if i leave out ‘eventDisplay’=>’upcoming’ but that allows past events to display, which i dont want.Heres the query
$featured_query = new WP_Query( array(
'post_type' => 'tribe_events',
'posts_per_page' => '1',
'orderby' => 'rand',
'eventDisplay'=>'upcoming',
'tax_query'=> array(
array(
'taxonomy' => 'tribe_events_cat',
'field' => 'slug',
'terms' => 'featured',
'operator' => 'IN'
)
)
)
);while ( $featured_query->have_posts() ) : $featured_query->the_post();
This code outputs the next event from the featured category, but the orderby rand seems to be ignored or overridden by eventDisplay. Am i missing something obvious here?
Appreciate any help you can give.February 17, 2012 at 1:52 pm #15341Jonah
ParticipantHi Paul, I’m going to get someone else to help you. Stay tuned.
February 17, 2012 at 5:01 pm #15345Joey Kudish
MemberHey Paul,
The following code should get you all set: https://gist.github.com/1856644 I’ve tested and it does exactly what you want.
Let us know if you have any other questions
JoeyFebruary 18, 2012 at 2:53 am #15375Paul
ParticipantHi Joey,
Thanks for your input on this, really appreciate your help. Unfortunately that code isn’t working for me.
Heres what i have http://db.tt/r0mVAOmf as before it is just outputting the next featured event.Having
die(var_dump($post));in there caused an error.Have i got something wrong?
February 18, 2012 at 12:40 pm #15378Joey Kudish
MemberHi Paul,
the die(var_dump($post)); was just there as an example, it basically output all of the contents of the $post variable.
A few notes on your full code:
— no need for that global $sp_ecp in the top, that’s from an old (pre 2.0) version of The Events Calendar
— sp_get_option is also a function from the old (pre 2.0) version of The Evetns Calendar
— you don’t need the query_posts before your first query since you are already at the homepage, the default query will already query for that page
— you can use the_permalink() and the_title() instead of using the $post variable
— no need to check if the has_post_thumbnail function exists unless you plan on running a pre 2.9 version of WordPress
— you should use wp_reset_query() instead of wp_reset_postdata()
— where you have upcoming-events in the sidebar it doesn’t fully make sense you’re checking if there’s a dynamic_sidebar called ‘Homepage Upcoming Events’ but if it does, you’re not doing anything; I’ve removed it from my example for now
— I’ve changed featured-event to a instead of a since it wasn’t a listAnyways, none of these little things were why the query wasn’t working for you. In fact it was because setting ‘eventDisplay’ => ‘upcoming’ was overwriting the ‘orderby’ => ‘rand’ argument. So instead you need to use the ‘meta_query’ and fetch upcoming events by their ‘_EventStartDate’
here’s the corrected example with all the code changes I specified above: https://gist.github.com/1860947
I tested with a few events in the featured category (some being past and others in the present) and it worked as expected for me. This should definitely get you going.
Cheers
JoeyFebruary 18, 2012 at 12:40 pm #15379Joey Kudish
MemberWoops, one of my comments got mangled, I meant:
– I’ve changed featured-event to a div instead of a ul since it wasn’t a list
February 23, 2012 at 6:39 am #15547Paul
ParticipantHi Joey,
That last update did the trick, fantastic, thanks for your help with this.Cheers
PaulFebruary 23, 2012 at 9:59 am #15568Rob
MemberExcellent! Thanks for confirming, Paul. Glad we were able to help you out here. If you need anything else down the road please let us know.
July 4, 2015 at 5:00 pm #975699Support Droid
KeymasterThis topic has not been active for quite some time and will now be closed.
If you still need assistance please simply open a new topic (linking to this one if necessary)
and one of the team will be only too happy to help. -
AuthorPosts
- The topic ‘Random Featured event – Custom Widget’ is closed to new replies.
