Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Working on upcoming events list that returns posts fine in a custom query. In the custom post type 'event' there are multiple events within a date range. I want to display all upcoming events in order. This I can do but I also want to count the events for each day.

Right now I am using found_posts to return the post count from the query but that misses posts that have two or more meta_keys filled in. How do I get an accurate count of events for both meta_keys - opening_time and artist_talk time?

The goal is to publish one custom post for an event that has a permalink with all the information. Each custom post has meta boxes that can be filled it with the date range for the beginning and end of the event (usually runs for a month or two) as well as dates for activities that are one day only during the run of the event. Opening Night, Lectures, Films .... that have their own meta box fields. Not all events have all activities.

I want to have the upcoming event list count the number of "activities" that happen on a certain day not count the CPT events. Each activity will list its type as an H2 and then the title that will link to the permalink of the Event with all the information.

Thank you in advance.

      <?php
      $today = date("Y-m-d");
      $todaytext = date("l");


  for ($i=0; $i<7; $i++){
      $thedate = strtotime ( '+'.$i.' day' , strtotime ( $today ) ) ;
      $thedate = date ( 'Y-m-d' , $thedate );
      $thedatetext = strtotime ( '+'.$i.' day' , strtotime ( $todaytext ) ) ;
      $thedatetext = date ( 'l' , $thedatetext );

  $event_query = new WP_Query(
      array( 
        'post_type'   => 'event',
        'meta_key'    => 'opening_time',
        'orderby'     => 'meta_value',
        'order'       => 'asc',
        'meta_query'  => array(
           array(
            'key'     => 'opening_time',
            'value'   => $thedate,
            'compare' => 'LIKE',
            'type'    => 'DATE'
          ) // end array
         ) // end 'meta_query' arrayx
        ) // end array
      ); // end top array 

  if ($event_query->have_posts()){

  echo $event_query->found_posts;   
  $totalevents=$event_query->found_posts;

  if ($totalevents>=2){
  echo ' Events ';
  } // end if ($totalevents>=2) function

  if ($totalevents<2){ 
  echo ' Event ';
  }

  if ($thedate==$today){echo 'Today';}
  if ($thedate>$today){
  echo $thedatetext;}
  echo  '<br>';
  }

  if ($event_query->have_posts()) : while ($event_query->have_posts()) :  $event_query->the_post();

  if (get_post_meta($post->ID,'opening_time', true)==$thedate)
  {
  ?>
  <a href="<?php the_permalink() ?>" title="<?php the_title(); ?>"><?php the_title('<h2>', '</h2>'); ?></a>
  <h2>Opening Reception</h2>
  <?php

  }

  if (get_post_meta($post->ID,'artist_talk_time', true)==$thedate){ ?>
  <a href="<?php the_permalink() ?>" title="<?php the_title(); ?>"><?php the_title('<h2>', '</h2>'); ?></a>
  <h2> Artist Talk </h2>

  } 

  endwhile;
  endif;
  rewind_posts();

  }
  ?>
share|improve this question
    
The available meta_keys for "activities" now are - opening_time, artist_talk_time, film_time –  John Bentwin Nov 13 '11 at 16:12

1 Answer 1

up vote 0 down vote accepted

I came up with a way using two post queries. The first one runs through the loop and adds 1 to a variable called $counttest. Then it echos that value and resets it to 0 to begin the count for the next date.

This probably isn't the most efficient way to do things. If anyone knows a better way then please let me know.

     <?php
      $today = date("Y-m-d");
      $todaytext = date("l");

  for ($i=0; $i<7; $i++){
      $thedate = strtotime ( '+'.$i.' day' , strtotime ( $today ) ) ;
      $thedate = date ( 'Y-m-d' , $thedate );
      $thedatetext = strtotime ( '+'.$i.' day' , strtotime ( $todaytext ) ) ;
      $thedatetext = date ( 'l' , $thedatetext );


$args = array(
 'post_type' => 'event',
 'orderby'     => 'meta_value',
 'order'       => 'asc',
 'meta_query' => array(
 'relation' => 'OR',
    array(
        'key' => 'opening_time',
        'value' => $thedate,
        'compare' => 'LIKE',
        'type' => 'DATE'
    ),
    array(
        'key' => 'artist_talk_time',
        'value' => $thedate,
        'compare' => 'LIKE',
        'type' => 'DATE'    
    )
)
  );

 $event_query = new WP_Query( $args );      

if ($event_query->have_posts()) : while ($event_query->have_posts()) :  $event_query->the_post(); 

if (get_post_meta($post->ID,'opening_time', true)==$thedate)
  { $counttest++;}

if (get_post_meta($post->ID,'film_time', true)==$thedate)
  { $counttest++;}

if (get_post_meta($post->ID,'artist_talk_time', true)==$thedate)
  { $counttest++;}

endwhile;

  echo $counttest;

  if ($counttest>=2){
  echo ' Events ';
  } // end if ($totalevents>=2) function

  if ($counttest<2){ 
  echo ' Event ';
  }
  echo $count;
  if ($thedate==$today){echo 'Today';}
  if ($thedate>$today){
  echo $thedatetext;}
  echo  '<br>';


endif;
$counttest=0;



  if ($event_query->have_posts()) : while ($event_query->have_posts()) :  $event_query->the_post();

  if (get_post_meta($post->ID,'opening_time', true)==$thedate)
  {
  ?>
  <a href="<?php the_permalink() ?>" title="<?php the_title(); ?>"><?php the_title('<h2>', '</h2>'); ?></a>
  <h2>Opening Reception</h2>
  <?php
  echo get_post_meta($post->ID,'opening_time', true);

  }

  if (get_post_meta($post->ID,'artist_talk_time', true)==$thedate){ ?>
  <a href="<?php the_permalink() ?>" title="<?php the_title(); ?>"><?php the_title('<h2>', '</h2>'); ?></a>
  <h2> Artist Talk </h2>
  <?php 
  echo get_post_meta($post->ID,'artist_talk_time', true);

  } 

  endwhile;

  endif;
  rewind_posts();

  }
  ?>
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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