I need to list the next seven days no matter what day it is. If its monday, it starts with today and lists 7 days, if its friday it starts with today and lists 7 days from today.

Next I need to show any events for that day. IF there are no events for a given day it simply lists a default graphic.

I am going to make a featured event for a each day and eventually set it up so that it just shows that one but for now I just want to list the next 7 days and any events contained within them.

I have two piece that don't fit together very well. First:

<?php $cur_day = 'current_day'; $last = 'last';
$date = date('o-m-d'); $ts = strtotime($date); $dow = date('w', $ts); 
for ($i=0; $i<7; $i++, $ts+=86400){ ?>

This will list each of the next 7 days starting with today.

 $event_query = new WP_Query(
          'post_type'   => 'event',        // only query events
          'meta_key'    => 'event-date',  // load up the event_date meta
          'orderby'     => 'meta_value',
          'order'       => 'asc',         // ascending, so earlier events first
          'meta_query'  => array(
             array(         // restrict posts based on meta values
              'key'     => 'event-date',  // which meta to query
              'value'   => $event_date,  // value for comparison
              'compare' => '=',          // method of comparison
              'type'    => 'NUMERIC'         // datatype, we don't want to compare the string values
            ) // meta_query is an array of query ites
           ) // end meta_query array
          ) // end array
        ); // close WP_Query constructor call

This lists the next seven days but doesn't do well with the events...

I am looking to duplicate this: http://www3.dmagazine.com/events with cpt's



You should definitely do it in just a single query. then set up a simple loop, each iteration get that day's date. then loop through all posts and compare the date to the date meta field, and output if it matches. I think something like this should work...

$today = date("o-m-d");
$future = strtotime ( '+6 days' , strtotime ( $today ) ) ;
$future = date ( 'o-m-d' , $future );
$event_query = new WP_Query(
      'post_type'   => 'event',
      'meta_key'    => 'event-date',
      'orderby'     => 'meta_value',
      'order'       => 'asc',
      'meta_query'  => array(
          'key'     => 'event-date',
          'value'   => array($today,$future),
          'compare' => 'BETWEEN',
          'type'    => 'DATE'

for ($i=0; $i<7; $i++){
    $thedate = strtotime ( '+'.$i.' day' , strtotime ( $today ) ) ;
    $thedate = date ( 'o-m-d' , $thedate );
    echo $thedate;
    // loop thru all posts and check $thedate against your date meta
    // and output if it matches
    // then rewind_posts(); to set it up for the next day
  • So the initial QP_Query is getting all the events that fit between today and the 7 days from now. Then if I hear you right, I need to run a simple query_posts loop inside of the for loop that is currently echoing the dates and compare $thedate to event-date. IS that what you have in mind? – TJ Sherrill Jun 6 '11 at 14:18

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.