我有一个事件的自定义帖子类型,带有一个显示event_date的字段。 目前,我的代码拉入了每个事件。 我只需要提取未通过的事件。
关于我如何才能选择将来发布的帖子的任何建议? 也就是说,只显示即将到来的事件。
到目前为止我的代码(忽略谷歌地图部分)...
<?php
$mapposts = new WP_Query( array(
'post_status' => 'publish',
'post_type' => 'tour-date',
'posts_per_page' => -1
) );
?>
<div class="acf-map">
<?php while ( $mapposts->have_posts() ) : $mapposts->the_post(); ?>
<?php
$location = get_field('event_map');
$gtemp = explode (',', implode($location));
$coord = explode (',', implode($gtemp));
?>
<div class="marker" data-lat="<?php echo $location[lat]; ?>" data-lng="<?php echo $location[lng]; ?>">
<h1><?php if(get_field('event_date'))
{
$date = DateTime::createFromFormat('Ymd', get_field('event_date'));
echo $date->format('M j');
}
?></h1>
<h2><?php the_title(); ?></h2>
</div>
<?php endwhile; ?>
</div><!-- .acf-map -->
<?php
$args=array(
'post_type' => 'tour-date',
'orderby'=> 'event_date',
'order' => 'ASC'
);
$my_query = null;
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
echo '';
$i = 0;
while ($my_query->have_posts()) : $my_query->the_post();
if($i % 3 == 0) { ?>
<?php
}
?>
<div class="box-shadow">
<ul class="event">
<li class="date">
<?php if(get_field('event_date'))
{
$date = DateTime::createFromFormat('Ymd', get_field('event_date'));
echo $date->format('M j');
}
?>
</li>
<li class="location"><?php the_field('event_city'); ?></li>
<li class="venue"><?php the_field('event_venue'); ?></li>
<li class="country"><?php the_field('event_country'); ?></li>
<li class="details"><a href="<?php the_permalink(); ?>">+ Info</a></li>
<li class="purchase"><a href="<?php the_field('event_tickets'); ?>" class="purchase" target="_blank">Tickets</a></li>
</ul>
</div>
<!-- /box-shadow -->
<?php
if($i % 3 == 0) { ?>
<?php
}
$i++;
endwhile;
}
wp_reset_query();
?>
答案 0 :(得分:1)
这应该是
<?php
$events = get_posts(array(
'post_type' => 'tour-date',
'posts_per_page'=> -1,
'meta_key' => 'event_date',
'orderby' => 'meta_value',
'order' => 'ASC'
));
foreach($events as $event){
$event_date = get_post_custom_values('event_date',$event->ID);
$event_date = $event_date[0];
list($y,$m,$d) = explode('-',$event_date);
$ym = $y.'-'.$m;
if($event_date<date('Y-m-d')) continue;
?>
答案 1 :(得分:1)
假设您将日期存储为时间戳,可以在查询中添加meta_compare
参数...
<?php
$args = array(
'post_type' => 'tour-date',
'posts_per_page' => -1,
'meta_key' => 'event_date',
'meta_value' => current_time( 'timestamp' ),
'meta_compare' => '>=',
'orderby' => 'meta_value',
'order' => 'ASC'
);
给它一个去
丹
答案 2 :(得分:0)
经过一段时间的游戏,我想出了这个有效的解决方案......
$event1 = current_time('Ymd');
$args = array(
'post_type' => 'tour-date',
'post_status' => 'publish',
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => 'event_date',
'compare' => '>=',
'value' => $event1,
)
),
'meta_key' => 'event_date',
'orderby' => 'meta_value',
'order' => 'ASC',
);
答案 3 :(得分:0)
这是根据我的上述方法编写代码的方式。但我同意dabbharami调用所有帖子的问题。你的方法看起来不错,所以我想你将不再需要这个
<?php
$events = get_posts(array(
'post_type' => 'tour-date',
'posts_per_page'=> -1,
'meta_key' => 'event_date',
'orderby' => 'meta_value',
'order' => 'ASC'
));
$i=0;
foreach($events as $event){
$event_date = get_post_custom_values('event_date',$event->ID);
$event_date = $event_date[0];
list($y,$m,$d) = explode('-',$event_date);
$ym = $y.'-'.$m;
if($event_date<date('Y-m-d')) continue;
?>
<div class="box-shadow">
<ul class="event">
<li class="date">
<?php if(get_field('event_date'))
{
$date = DateTime::createFromFormat('Ymd', get_field('event_date'));
echo $date->format('M j');
}
?>
</li>
<li class="location"><?php the_field('event_city'); ?></li>
<li class="venue"><?php the_field('event_venue'); ?></li>
<li class="country"><?php the_field('event_country'); ?></li>
<li class="details"><a href="<?php the_permalink(); ?>">+ Info</a></li>
<li class="purchase"><a href="<?php the_field('event_tickets'); ?>" class="purchase" target="_blank">Tickets</a></li>
</ul>
</div>
<!-- /box-shadow -->
<?php
$i++;
if($i==3) break;
}
?>