最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - Modern Tribe Calendar wp-query with meta query not working at all

programmeradmin3浏览0评论

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

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

Share Improve this question asked Dec 20, 2016 at 12:20 blackhill24blackhill24 1271 silver badge5 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 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();

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.

发布评论

评论列表(0)

  1. 暂无评论