填写了meta_key的帖子计数?

时间:2011-11-13 作者:John Bentwin

处理即将发生的事件列表,该列表在自定义查询中返回帖子。在自定义帖子类型“event”中,一个日期范围内有多个事件。我想按顺序显示所有即将发生的事件。这是我可以做到的,但我还想统计每天发生的事件。

现在,我正在使用find\\u posts从查询中返回post计数,但这会遗漏填充了两个或更多meta\\u键的帖子。如何获得meta\\u键-Open\\u time和Artister\\u talk time的事件准确计数?

目标是为具有永久链接的事件发布一篇自定义帖子,其中包含所有信息。每个自定义帖子都有元框,可以用事件开始和结束的日期范围(通常运行一个月或两个月)以及仅在事件运行期间一天的活动日期填充。开幕之夜、讲座、电影。。。。有自己的元框字段。并非所有事件都有所有活动。

我想让即将到来的事件列表计算某一天发生的“活动”的数量,而不计算CPT事件。每个活动将以H2的形式列出其类型,然后列出标题,该标题将链接到包含所有信息的事件的永久链接。

提前谢谢你。

      <?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();

  }
  ?>

1 个回复
最合适的回答,由SO网友:John Bentwin 整理而成

我想出了一种使用两个post查询的方法。第一个循环遍历循环,并将1添加到名为$counttest的变量中。然后它回显该值并将其重置为0,以开始下一个日期的计数。

这可能不是最有效的做事方式。如果有人知道更好的方法,请告诉我。

     <?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();

  }
  ?>

结束

相关推荐

Only Showing Upcoming Events

在此页面的侧栏中:http://lifebridgecypress.org/our-people, 我有一个即将使用此代码的事件列表。。。<ul id=\"upcoming-events\"> <?php $latestPosts = new WP_Query(); $latestPosts->query(\'cat=3&showposts=10\'); ?> <?php while ($latestPos