orderby rand not working correctly

Home Forums Calendar Products Events Calendar PRO orderby rand not working correctly

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #1163794
    blankbeard
    Participant

    I am trying to display random events on my front page, in a text widget. It works, but not completely. It seems to be randomly cycling through only the upcoming 5-10 events (I have 50+ events a month).
    I didn’t know if there was something in the plugin that is preventing orderby rand from working correctly. I thought I’d ask here before I go get yelled at in public forums.

    Here is the code I have in a text widget.

    <?php remove_all_filters('posts_orderby'); 
    $args=array('post_type'=>'tribe_events', 'orderby'=>'rand', 'posts_per_page'=>'1', 'ignore_sticky_posts' => 1); 
    $projects=new WP_Query($args); 
    while ($projects->have_posts()) : $projects->the_post(); ?>
     <?php if ( has_post_thumbnail() ) {
    	the_post_thumbnail();
    }  ?>
     <h2><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h2>
     <p><?php $itswhen = tribe_get_start_date (); echo $itswhen; ?></p>
    <p><?php the_excerpt();?></p> 
    <?php endwhile; wp_reset_postdata(); ?>

    Thanks

    #1164321
    Josh
    Participant

    Hey blankbeard,

    Thanks for reaching out to us!

    The first thought I had was that this could potentially be a limitation set by your host. For example, if you host your website with WP Engine, they will disable the ability for random order on WP_Query by default and you would need to enable it manually within their settings.

    However, it sounds like generally the ordering is pulling randomly but not from your full event library?

    One thing that I would start with for testing here would be to include a couple of extra arguments to your query. Try adding:

    
    'suppress_filters' => true,
    'eventDisplay' => 'upcoming'
    

    Let me know if this helps.

    Thanks!

    #1164431
    blankbeard
    Participant

    That definitely did the job, but a little too well. I am now getting events that have passed. Also, if possible I’d like to only get the next upcoming recurring event, it is showing an event from November, even though there are 2 in October.

    Is there somewhere I can go for all of the possible args? I am a little green still, but I don’t want to clog up the forums if there is documentation somewhere that I can fumble through.

    Thanks so much!

    #1164538
    Josh
    Participant

    Hey blankbeard,

    Thanks for following up! I’m glad we were at least able to see some movement here.

    You could switch to using the “tribe_get_events()” function which is a wrapper for “WP_Query” there. That will allow things like the “eventDisplay” => ‘upcoming’ to work properly there. You can find more on the options there and how to us it here https://theeventscalendar.com/knowledgebase/using-tribe_get_events/.

    Also, when using the tribe_get_events() function, you can also use ‘tribeHideRecurrence’ => 1 to pull only the next upcoming event within the recurring series.

    Let me know if this helps.

    Thanks!

    #1173777
    Support Droid
    Keymaster

    Hey there! This thread has been pretty quiet for the last three weeks, so we’re going to go ahead and close it to avoid confusion with other topics. If you’re still looking for help with this, please do open a new thread, reference this one and we’d be more than happy to continue the conversation over there.

    Thanks so much!
    The Events Calendar Support Team

Viewing 5 posts - 1 through 5 (of 5 total)
  • The topic ‘orderby rand not working correctly’ is closed to new replies.