Random Featured event – Custom Widget

Home Forums Calendar Products Events Calendar PRO Random Featured event – Custom Widget

Viewing 15 posts - 1 through 15 (of 15 total)
  • Author
    Posts
  • #15257
    Paul
    Participant

    Im 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?
    Cheers

    #15283
    Rob
    Member

    Hey Paul. Interesting question; let me see if Jonah has any ideas when he hits the forum this afternoon.

    #15298
    Jonah
    Participant

    Hey 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.

    #15310
    Paul
    Participant

    Hey 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');
    }

    #15311
    Paul
    Participant

    Im unsure how to replace tribe_get_events with WP_Query correctly.

    #15332
    Jonah
    Participant

    Hey 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.

    #15339
    Paul
    Participant

    Hi 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.

    #15341
    Jonah
    Participant

    Hi Paul, I’m going to get someone else to help you. Stay tuned.

    #15345
    Joey Kudish
    Member

    Hey 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
    Joey

    #15375
    Paul
    Participant

    Hi 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?

    #15378
    Joey Kudish
    Member

    Hi 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 list

    Anyways, 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
    Joey

    #15379
    Joey Kudish
    Member

    Woops, 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

    #15547
    Paul
    Participant

    Hi Joey,
    That last update did the trick, fantastic, thanks for your help with this.

    Cheers
    Paul

    #15568
    Rob
    Member

    Excellent! 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.

    #975699
    Support Droid
    Keymaster

    This 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.

Viewing 15 posts - 1 through 15 (of 15 total)
  • The topic ‘Random Featured event – Custom Widget’ is closed to new replies.