1

I'm working with an upcoming events list. I create a custom post type, with a custom field: [event_start_date]. I also have some multiday extension events, defined by an additional custom field: [event_end_date]. Then, I have this two kinds of events (examples):

  • Birthday | start date: 13/08/2012, end_date: 'empty'
  • Holidays | start date: 5/08/2012, end_date: 30/08/2012

I would like to display events with a 'start' or 'end date' later than current time, but I failed.

I think my date format is correct, because I had started using other 'query' that works perfect only using the [start date]. My problem is incorporating the 'OR' end_date condition. I tried with a meta_query with an 'OR relation', but doesn't work:

$args = array(
             'post_type' => 'eventos2',
             'posts_per_page' => 4,
             'meta_key' => 'event_start_date',
             'orderby' => 'meta_value',
             'order' => 'ASC',
             'meta_query' => array(
             'relation' => 'OR',
             array('key'  => 'event_start_date',
                   'value' => date('Y/m/d', strtotime('-1 day')),
                   'compare' => '>=')

             array('key'  => 'event_end_date',
                   'value' => date('Y/m/d', strtotime('-1 day')),
                   'compare' => '>=')
             )
   );
$loop = new WP_Query( $args ); 
while ( $loop->have_posts() ) : $loop->the_post();
$end = DateTime::createFromFormat('Y/m/d', get_field('event_end_date')); 
$start = DateTime::createFromFormat('Y/m/d', get_field('event_start_date'));

if ($end !='') {  /*print events with start and end_date*/ 
    echo "<li>";
    echo the_title(), "</br>";
    echo $start-> format('Y/m/d'),"&nbsp". to . "&nbsp". $end-> format('Y/m/d'); 
    echo "</li>";
    }
    else {             /*print events with only start_date*/
    echo "<li>";
    echo the_title(), "</br>";
    echo $start-> format('Y/m/d');
    echo "</li>";   
    }
    endwhile;
   ?>

It show this result:

  • EVENT 2 | 2012/08/08 to 2012/08/15
  • EVENT 4 | 2012/08/31 to 2012/09/06
  • EVENT 3 | 2012/08/17
  • EVENT 1 | 2012/08/01

How you can see, EVENT 1 is listed, despite of is a past event. Moreover events are in disorder.

I will appreciate any orientation with the problem.

3
  • What goes wrong? Do events appear that shouldn't, or events that should don't appear? Commented Aug 12, 2012 at 16:33
  • It shows all the events list, including past events. However if I delete the 'end date' condition, it works perfect only using start date. Thanks for response.
    – Inao V.
    Commented Aug 12, 2012 at 16:41
  • Hi Inao V. See my updated answer. I suspect the relation=>'OR' problem is only part of the problem. Commented Aug 12, 2012 at 17:30

1 Answer 1

1

Edit:

I believe my comments below are still valid, but I suspect part of the problem is the

'relation' => 'OR',

This should be 'AND' (it's default value,so it can be removed) to ensure that it only returns posts which satisfy both conditions. Currently it will return events that start, or end, after yesterday. In effect, this returns all events that end yesterday, or after.

Of course this may still only be part of the problem...


I can't see anything wrong with the query itself, but the string representation of your events' dates will cause query problems (without examples it's hard to say if this is the problem). If you are going to sort by a string representation of dates rather than a timestamp, the format m/d/Y won't sort properly:

01/01/2012 occurs after, but appears before

11/01/1012

for instance! The format Y/m/d will work.

5
  • Thanks Stephen. I tried changing to the format Y/m/d, but it also show me past events. I also tried AND instead of OR, and removing it, but it only show events that have end date. If you know another more appropiate way to achieve this. I will appreciate. I will update the problem with an example of my two types of events.
    – Inao V.
    Commented Aug 12, 2012 at 17:49
  • And updated the posts to use that format? Bear in mind that Y/m/d in php format is 2012/08/12 (zeroes are important). If you are querying a meta key, it will only include posts which have that key. Other than that, the code looks good, and its hard to say what's wrong without real examples. Commented Aug 12, 2012 at 18:27
  • Yes zeros included, In fact I create a new post type to test this format. Is there any way to show you real examples?
    – Inao V.
    Commented Aug 12, 2012 at 18:59
  • Yup, edit your question to list the events start/end dates, in the order they appear. Keep in mind the effect of timezones. Also, I somehow missed the examples you have given - are you using the string 'empty' or just not giving it an end date? Commented Aug 12, 2012 at 19:08
  • Stephen, I updated the question as you indicated. I'm just not given a end date, for one day duration events. thanks!
    – Inao V.
    Commented Aug 12, 2012 at 21:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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