无法使用多个元关键字按日期/时间对wp_Query进行排序

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

4th EDIT 11/17/2011

我说得太快了。现在我有了所有的帖子,但在我移动了endif之后,总事件数并没有按天将事件分开;和endwhile;我把修改过的代码贴在下面,下面是以前修改过的代码。

之前,每天的计数都是正确的。如果我周五有两个来自不同帖子的事件,那么它会说“周五有两个事件”,但它只会显示最近帖子的内容。

现在它显示了所有帖子的内容,但如果我周五有两个来自不同帖子的事件,那么在每个帖子之前都会显示“1个事件周五”。

这是代码,谢谢你。

<?php
      $today = date("Y-m-d 00:00");  //set today\'s date 
      $todaytext = date("l"); // define $todaytext with written out days

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

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

$event_query = new WP_Query( $args ); 

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

$keys = array(\'opening_time\', \'film_time\', \'artist_talk_time\');
$times = array();  // I tried putting the new array line here but then I got a repeat of the post for everyday
$custom_field_keys = get_post_custom_keys();
foreach ($custom_field_keys as $custom_field_key) {

if (in_array($custom_field_key, $keys)) 
{
    $custom_field_value = get_post_meta($post->ID, $custom_field_key, true);

    if ($custom_field_value >= $thedate && $custom_field_value <= $future) {

        $counttest++;
        $times[] = array($custom_field_value, $post->ID, $custom_field_key); // I changed the array here

    }
    }
    }     


 if ($counttest >0) {
  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>\';

  }

  $counttest=0;


  asort($times); // I changed the asort to sort here
  foreach ($times as $event) { $time_value = $event[0]; $post_id = $event[1]; $time_key = $event[2];  // changed the foreach here

if ($time_key == \'opening_time\') { ?>

<li class="sidebar_event">
<a href="<?php echo get_permalink($post_id) ?>" title="<?php echo get_the_title($post_id); ?>"> <?php echo get_the_post_thumbnail($post_id, \'upcoming_event_sidebar\'); ?></a> 

<h2><a href="<?php echo get_permalink($post_id) ?>" title="<?php echo get_the_title($post_id); ?>"> <?php echo get_the_title($post_id); ?></a></h2> 
<h3>Opening</h3>
<h4><?php echo get_post_meta($post_id,\'event_venue\', true);?> </h4>
<h5><?php echo get_post_meta($post_id,\'opening_time\', true);?> </h5>
</li><!-- end .sidebar_event -->
<?php }

else if ($time_key == \'artist_talk_time\') { ?>

<li class="sidebar_event">
<a href="<?php echo get_permalink($post_id) ?>" title="<?php echo get_the_title($post_id); ?>"> <?php echo get_the_post_thumbnail($post_id, \'upcoming_event_sidebar\'); ?></a> 

<h2><a href="<?php echo get_permalink($post_id) ?>" title="<?php echo get_the_title($post_id); ?>"> <?php echo get_the_title($post_id); ?></a></h2> 
<h3>Artist Talk</h3>
<h4><?php echo get_post_meta($post_id,\'event_venue\', true);?> </h4>
<h5><?php echo get_post_meta($post_id,\'artist_talk_time\', true);?> </h5>
</li><!-- end .sidebar_event -->
<?php }

  else if ($time_key == \'film_time\') {
        ?>
  <h2><a href="<?php echo get_permalink($post_id) ?>" title="<?php echo get_the_title($post_id); ?>">
  <?php echo get_the_title($post_id); ?></a></h2> 
  <h2>Film</h2>
  <?php
  echo get_post_meta($post_id,\'film_time\', true); echo get_the_content($post_id); 
 }

 }
     endwhile;
  endif;

  rewind_posts();

  }
  ?>

End 4th EDIT

3rd EDIT 11/17/2011我做了你建议的改变,但行为保持不变。我试着做一些其他的改变,我可以把我的听力包起来,但我知道运气。接下来你会尝试什么?非常感谢你的帮助。

revised code

  <?php
      $today = date("Y-m-d 00:00");  //set today\'s date 
      $todaytext = date("l"); // define $todaytext with written out days

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

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

$event_query = new WP_Query( $args ); 

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

$keys = array(\'opening_time\', \'film_time\', \'artist_talk_time\');
$times = array();  // I tried putting the new array line here but then I got a repeat of the post for everyday
$custom_field_keys = get_post_custom_keys();
foreach ($custom_field_keys as $custom_field_key) {

if (in_array($custom_field_key, $keys)) 
{
    $custom_field_value = get_post_meta($post->ID, $custom_field_key, true);

    if ($custom_field_value >= $thedate && $custom_field_value <= $future) {

        $counttest++;
        $times[] = array($custom_field_value, $post->ID, $custom_field_key); // I changed the array here

    }
    }
    }     
 endwhile;

 if ($counttest >0) {
  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;


 sort($times); // I changed the asort to sort here
 foreach ($times as $event) { $time_value = $event[0]; $post_id = $event[1]; $time_key = $event[2];  // changed the foreach here

 if ($time_key == \'opening_time\') {
    ?>
  <h2><a href="<?php echo get_permalink($post_id) ?>" title="<?php echo get_the_title($post_id); ?>">
  <?php echo get_the_title($post_id); ?></a></h2> 
  <h2>Opening</h2>
  <?php
  echo get_post_meta($post_id,\'opening_time\', true); echo get_the_content($post_id); 

  }
  else if ($time_key == \'artist_talk_time\') {
        ?>
  <h2><a href="<?php echo get_permalink($post_id) ?>" title="<?php echo get_the_title($post_id); ?>">
  <?php echo get_the_title($post_id); ?></a></h2> 
  <h2>Artist Talk</h2>
  <?php
  echo get_post_meta($post_id,\'artist_talk_time\', true); echo get_the_content($post_id); 

 }
  else if ($time_key == \'film_time\') {
        ?>
  <h2><a href="<?php echo get_permalink($post_id) ?>" title="<?php echo get_the_title($post_id); ?>">
  <?php echo get_the_title($post_id); ?></a></h2> 
  <h2>Film</h2>
  <?php
  echo get_post_meta($post_id,\'film_time\', true); echo get_the_content($post_id); 
 }

 }

  rewind_posts();

  }
  ?>

end 3rd EDIT

2nd EDIT 11/16/2011Marcochiesi,谢谢你的额外帮助。现在我得到了正确的事件计数,并且它们被正确地排序。问题是,只有最近的帖子才显示任何内容。如果我添加一个新的CPT事件,那么它将显示其标题和永久链接,但如果我删除它,它将返回到下一篇最新的帖子。

你知道问题出在哪里吗?再次感谢您。没有你的帮助是不可能的。

  <?php
      $today = date("Y-m-d 00:00");  //set today\'s date 
      $todaytext = date("l"); // define $todaytext with written out days

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

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

$event_query = new WP_Query( $args ); 

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

$keys = array(\'opening_time\', \'film_time\', \'artist_talk_time\');
$times = array();
$custom_field_keys = get_post_custom_keys();
foreach ($custom_field_keys as $custom_field_key) {

if (in_array($custom_field_key, $keys)) 
{
    $custom_field_value = get_post_meta($post->ID, $custom_field_key, true);

    if ($custom_field_value >= $thedate && $custom_field_value <= $future) {

        $counttest++;

        $times[$custom_field_key] = array($custom_field_value, $post->ID);
    }
    }
    }     
 endwhile;
 if ($counttest >0) {
  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;


 asort($times);
 foreach ($times as $time_key => $event) {
 $time_value = $event[0]; $post_id = $event[1]; 

 if ($time_key == \'opening_time\') {
    ?>
  <h2><a href="<?php echo get_permalink($post_id) ?>" title="<?php echo get_the_title($post_id); ?>">
  <?php echo get_the_title($post_id); ?></a></h2> 
  <h2>Opening</h2>
  <?php
  echo get_post_meta($post_id,\'opening_time\', true); echo get_the_content($post_id); 

 }
 else if ($time_key == \'artist_talk_time\') {
        ?>
  <h2><a href="<?php echo get_permalink($post_id) ?>" title="<?php echo get_the_title($post_id); ?>">
  <?php echo get_the_title($post_id); ?></a></h2> 
  <h2>Artist Talk</h2>
  <?php
  echo get_post_meta($post_id,\'artist_talk_time\', true); echo get_the_content($post_id); 

 }
  else if ($time_key == \'film_time\') {
        ?>
  <h2><a href="<?php echo get_permalink($post_id) ?>" title="<?php echo get_the_title($post_id); ?>">
  <?php echo get_the_title($post_id); ?></a></h2> 
  <h2>Film</h2>
  <?php
  echo get_post_meta($post_id,\'film_time\', true); echo get_the_content($post_id); 
 }

 }

  rewind_posts();

  }
  ?>

END 2nd EDIT

EDIT 11/15/2011

在更改了marcochiesi(谢谢)的代码后,我能够流式处理我的if语句,并能够在opening\\u时间之前对artist\\u talk\\u时间进行排序,如果meta\\u键是这么说的话。

但现在,我有重复的帖子来自多天的排序。每个CPT可以有3个meta\\u键,其中包含日期和;时间值。

目前我有3个测试CPT。1今天2:00有一个开放时间(它首先出现),第二天2:00还有一个艺术家演讲时间。这在今天和明天都会出现。

所有帖子都会发生这种行为。我做错了什么?

revised code

 <?php
      $today = date("Y-m-d 00:00");  //set today\'s date 
      $todaytext = date("l");  // define $todaytext with written out days

 for ($i=0; $i<7; $i++){
      $thedate = strtotime ( \'+\'.$i.\' day\' , strtotime ( $today ) ) ;
      $thedate = date ( \'Y-m-d H:i\' , $thedate );
      $thedatetext = strtotime ( \'+\'.$i.\' day\' , strtotime ( $todaytext ) ) ;
      $thedatetext = date ( \'l\' , $thedatetext );
      $future = strtotime ( \'+23 hours\' , strtotime ( $thedate ) ) ;
      $future = date ( \'Y-m-d H:i\' , $future ); 
 $args = array(
 \'post_type\' => \'event\',
 \'orderby\'     => \'meta_value\',
 \'order\'       => \'asc\',
 \'meta_query\' => array(
 \'relation\' => \'OR\',
    array(
        \'key\' => \'opening_time\',
        \'value\' => array($today,$future),
        \'compare\' => \'BETWEEN\',
        \'type\' => \'DATE\'
    ),
    array(
        \'key\' => \'artist_talk_time\',
        \'value\' => array($today,$future),
        \'compare\' => \'BETWEEN\',
        \'type\' => \'DATE\'    
    )
)
);

$event_query = new WP_Query( $args ); 

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

$keys = array(\'opening_time\', \'film_time\', \'artist_talk_time\');
$times = array();
$custom_field_keys = get_post_custom_keys();

foreach ($custom_field_keys as $custom_field_key) {

if (in_array($custom_field_key, $keys)) 
{
    $custom_field_value = get_post_meta($post->ID, $custom_field_key, true);

    if ($custom_field_value >= $thedate && $custom_field_value <= $future) {

        $counttest++;

        $times[$custom_field_key] = $custom_field_value;
    }
 }
 }     
 endwhile;
 if ($counttest >0) {
  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();

  asort($times);
  foreach ($times as $time_key => $time_value) {

  if ($time_key == \'opening_time\') {
    ?>
  <a href="<?php the_permalink() ?>" title="<?php the_title(); ?>"><?php the_title(\'<h2>\', \'</h2>\'); ?></a>
  <h2>Opening</h2>

  <?php
  echo get_post_meta($post->ID,\'opening_time\', true);
  the_content();
  }
  else if ($time_key == \'artist_talk_time\') {
        ?>
  <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);
  the_content();
  }
  else if ($time_key == \'film_time\') {
        ?>
  <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,\'film_time\', true);
  the_content();
  }

  }

  endwhile;

  endif;
  rewind_posts();

  }
  ?>

END EDIT 11/15/2011 - Original Question Below

通过这个答案集合的帮助,我有下面的代码,可以按日期对事件进行排序,然后列出并统计它们。它不按时间过滤。

例如,一个CPT“event”具有多个meta\\u键,其中有一个yyyy-mm-dd 00:00条目。每个主要活动或艺术开幕式大约持续一个月,但也有开幕招待会和艺术家演讲会,它们有自己的meta\\u键opening\\u time和artist\\u talk\\u time。

我还有一个页面列出了长达一个月的活动部分。现在我试着按顺序列出即将发生的事件。

问题出在单个CPT“事件”中,如果Artister\\u talk\\u时间是16:00,而opening\\u时间是在16:00之后,则opening\\u时间显示在第一位,因为它在我显示\\u内容()的循环中被称为第一位。这不是我想要的行为。

一天中的所有事件,即使在同一CPT中,也应按时间顺序列出。

我确信我这样做是错误的,我们非常感谢您的帮助。

非常感谢。

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

  for ($i=0; $i<7; $i++){
      $thedate = strtotime ( \'+\'.$i.\' day\' , strtotime ( $today ) ) ;
      $thedate = date ( \'Y-m-d H:i\' , $thedate );
      $thedatetext = strtotime ( \'+\'.$i.\' day\' , strtotime ( $todaytext ) ) ;
      $thedatetext = date ( \'l\' , $thedatetext );
      $future = strtotime ( \'+23 hours\' , strtotime ( $thedate ) ) ;
      $future = date ( \'Y-m-d H:i\' , $future ); 
 $args = array(
 \'post_type\' => \'event\',
 \'orderby\'     => \'meta_value\',
 \'order\'       => \'asc\',
 \'meta_query\' => array(
 \'relation\' => \'OR\',
    array(
        \'key\' => \'opening_time\',
        \'value\' => array($today,$future),
        \'compare\' => \'BETWEEN\',
        \'type\' => \'DATE\'
    ),
    array(
        \'key\' => \'artist_talk_time\',
        \'value\' => array($today,$future),
        \'compare\' => \'BETWEEN\',
        \'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) && (get_post_meta($post->ID,\'opening_time\', true)<=$future))
  { $counttest++;}

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

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

endwhile;
  if ($counttest >0) {
  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) && (get_post_meta($post->ID,\'opening_time\', true)<=$future))
  {
  ?>
  <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);
  the_content();

  }

  if ((get_post_meta($post->ID,\'artist_talk_time\', true)>=$thedate) && (get_post_meta($post->ID,\'artist_talk_time\', true)<=$future))
  { ?>
  <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);
  the_content();

  } 

  endwhile;

  endif;
  rewind_posts();

  }
  ?>

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

这只是一个想法:您可以将时间值保存在一个数组中,然后使用PHP对其进行排序。示例:

$keys = array(\'opening_time\', \'film_time\', \'artist_talk_time\');
$times = array();
$custom_field_keys = get_post_custom_keys();
foreach ($custom_field_keys as $custom_field_key) {
    if (in_array($custom_field_key, $keys) {
        $custom_field_value = get_post_meta($post->ID, $custom_field_key, true);
        if ($custom_field_value >= $thedate && $custom_field_value <= $future) {
            $counttest++;
            $times[$custom_field_key] = $custom_field_value;
        }
    }
}
asort($times);
foreach ($times as $time_key => $time_value) {
    if ($time_key == \'opening_time\') {
        ...
    }
    else if ($time_key == \'film_time\') {
        ...
    }
    ...
}

结束

相关推荐

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