Home › Forums › Calendar Products › Events Calendar PRO › Trouble Getting tribe_get_events() to return future events
- This topic has 4 replies, 2 voices, and was last updated 10 years ago by Casey D.
-
AuthorPosts
-
May 1, 2014 at 12:39 pm #147672phylaxisParticipant
I’m working on a site where we have a custom “widget” where I want to display the first 5 upcoming events for the current week and then in another tab display the first 5 events for the next week. You can see a sample of the site here, http://sdgc.terraform.com. If you look at the ‘This Week’ and ‘Next Week’ tabs you can see the functionality. The ‘This week’ is working fine, but the ‘Next Week’ is empty. The “reason” is that it seems like the function is filtering the number of posts before it filters the date range. If I set the ‘posts_per_page’ to infinite (events) then the code works, but then I have too many events displayed in each tab, plus the query takes way too long to do it’s thing.
You can see my custom function code here: https://gist.github.com/11459477
And the calls to my function look like this:
This Week:sdgc_get_events();
Next Week:sdgc_get_events( strtotime("+6 day"), strtotime("+13 day") )
May 2, 2014 at 1:31 pm #148500Casey DMemberMay 2, 2014 at 10:09 pm #148675phylaxisParticipantCasey,
Thanks for the reply. I actually did finally end up getting it working the way I needed it. there were two things that were key that I’ll mention here in case others run into some of the same trouble.First, since I’m on a shared server and don’t have much control over the PHP config I needed to compensate for time zone. Luckily it’s easy to do reading the WordPress setting and setting the default timezone in my function:
// set the time to local
$timezone = get_option( 'timezone_string' );
date_default_timezone_set($timezone);
Second, I needed to set the
'eventDisplay'
argument to ‘all’ so that I could get each full days list of events, even if the event had passed for that day:$get_posts = tribe_get_events(array('start_date'=>$start_date,'end_date'=>$end_date, 'posts_per_page'=>10, 'eventDisplay'=>'all' ) );
Finally, because I wanted to be sure I had a full day (and I’m not 100% sure this step is needed) I added a ’00:00:00′ time to my start dates and a ’23:59:59′ time end dates to ensure I was covering from the start of the day to the end of the day.
$start_date = isset($start) ? date('Y-m-d 00:00:00', $start) : date('Y-m-d 00:00:00', strtotime('-1 day'));
$end_date = isset($end) ? date('Y-m-d 23:59:59', $end) : date('Y-m-d 23:59:59', strtotime('+6 day'));
After I made all those changes it now seems to be working as expected, including being able to use the
'posts_per_page'=>10
argument and get up to 10 events for each day in the correct range.May 2, 2014 at 10:12 pm #148678phylaxisParticipantOh, here is the final full function I used in case anyone finds it helpful:
function sdgc_get_events($start=null,$end=null) {
global $post;
// set the time to local
$timezone = get_option( 'timezone_string' );
date_default_timezone_set($timezone);
$start_date = isset($start) ? date('Y-m-d 00:00:00', $start) : date('Y-m-d 00:00:00', strtotime('-1 day'));
$end_date = isset($end) ? date('Y-m-d 23:59:59', $end) : date('Y-m-d 23:59:59', strtotime('+6 day'));
$get_posts = tribe_get_events(array('start_date'=>$start_date,'end_date'=>$end_date, 'posts_per_page'=>10, 'eventDisplay'=>'all' ) );printf('<table class="sdgc-events">');
printf('<tbody>');
foreach($get_posts as $post) {
setup_postdata($post);
$dt = tribe_get_start_date($post->ID, true, 'j M Y');
$cd = new DateTime($dt);
$ft = $cd->format('n/j');
if((strtotime($dt) > strtotime($cd)) && (strtotime($dt) < strtotime($end_date)))
{
printf('<tr><td class="sdgc-event-item"><span class="primary-color-light">%s</span> - <span class="sdgc-event-title">%s</span></td><td class="sdgc-chevron"><i class="fa fa-lg fa-chevron-circle-right"></i></td></tr>', get_permalink(), $ft, get_the_title(), get_permalink());
}
} //endforeachprintf('</tbody>');
printf('</table>');wp_reset_query();
}Then call the function like this:
<?php sdgc_get_events( strtotime("+1 day"), strtotime("+3 day") ); ?>
May 5, 2014 at 11:35 am #150598Casey DMemberHey this is great! Makes my job a whole lot easier 🙂
We’d love it if you would leave us a review and let people know how much you like the plugin! http://m.tri.be/jt
I’ll go ahead and close this thread. Let us know if we can help with anything else.
Cheers!
– Casey Driscoll
-
AuthorPosts
- The topic ‘Trouble Getting tribe_get_events() to return future events’ is closed to new replies.