2

I've been searching for a good few days now trying to get my WordPress event calendar by modern tribe working as I need it to.

I have hundreds of order by and delivery dates in DB each one separated by about 5-10 weeks. what I'm trying to achieve is a list where I can see the all the events starting on today's or yesterday's date.

This sounds simple however the problem appears to be when you query events between two dates it will query both the start and end date giving you historical events which don't need to be seen and can be confusing.

I believe the way to overcome this is with a wp_query and meta_query but no matter which way I try to add the meta query to sort by start date only it breaks the whole thing.

this is the query below any help would be awsome as I've little hair left to pull out!

 <?php
  $query = new WP_Query( array( 'post_type' => 'tribe_events',
    'meta_query' => array(
        array(
            'key' => '_EventStartDate',
            'value' => date('Y-m-d H:i:s', strtotime('-1 week')),
            'compare' => 'date'
        )
    )
  ) );
  if ($query->have_posts())
  {
    while ($query->have_posts()) : $query->the_post();
        echo $query->post->EventStartDate . ' ';
        echo $query->post->post_title . '</br>';

    endwhile;
  }
  wp_reset_query();
  ?>

I've also tried changing the meta value to

  'value' => date('Y-m-d', strtotime('-1 week')),

but this didnt work either...

Thanks

2 Answers 2

2

What I would do is create a query for the _EventStartDate with a custom 'eventDisplay'. This will grab all events, ordered by the start date. Then, once you get into the loop; compare the start date to a specific date that you want to output. tribe_get_start_date() accepts a date format parameter (see here) that will allow you to return the format that you would like to compare against. Note, This code is untested but should put you on the right track.

$args = array(
    'post_status'=>'publish',
    'post_type'=> 'tribe_events',
    'posts_per_page'=> 10,
    'meta_key'=> '_EventStartDate',
    'orderby'=> '_EventStartDate',
    'order'=> 'DESC',
    'eventDisplay'=> 'custom',
);

$query = new WP_Query($args);

if ($query->have_posts()) {
    while ($query->have_posts()) : $query->the_post();
        if(tribe_get_start_date() != [INSERT DATE TO COMPARE WITH]) {
            echo tribe_get_start_date() . ' ';
            the_title() . '</br>';
        }
    endwhile;
}

wp_reset_postdata();
1
  • I've tried your suggestions but unfortunately the query seems to ignore " if(tribe_get_start_date() != date('Y-m-d', strtotime('-1 week'))) {" and therefore returns all of the dates which is annoying. if youve any more suggestions im all ears... thanks again for your input Dec 22, 2016 at 11:03
2

I've been able to have success with this:

$query = new WP_Query( array(
    'post_type' => 'tribe_events',//Display only event post types
    'eventDisplay' => 'custom',//Needed to override tribe's modifications to the WP_Query
    'order' => 'ASC',//Order events by the ones closest to today first
    'orderby' => '_EventStartDate',//Order events using their start date
    'meta_query' => array( array(
        'key' => '_EventStartDate',//Compare using the event's start date
        'value' => date('Y-m-d H:i:s'),//Compare against today's date
        'compare' => '>=',//Get events that are set to the value's date or in the future
        'type' => 'DATE'//This is a date query
    ) )
) );

And if you're using the pre_get_posts hook, I'm also setting the "post__in" value to an empty array because I think tribe did some stuff to modify the query already to populate that list with post ID's.

1
  • thank you for this! For people looking to add this into the query args with a conditional statement this is what I am using: $meta_query[] = array( array(.... )); $args['meta_query'] = $meta_query;
    – tiadotdev
    May 16 at 22:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.