Reputation: 1220
I have a custom post type for events, with a field to display the event_date. At the moment, my code pulls in EVERY event. I need to to only pull in events that have NOT passed.
Any advice on how I can only select posts that occur in the future? That is, show only UPCOMING EVENTS.
My code so far (ignore the google maps part)...
<?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();
?>
Upvotes: 0
Views: 1871
Reputation: 328
this is how the code shoud be written according to my above method. but i agree with dabbharami the issue with calling all posts. u r method looks good so i think u will no longer need this
<?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;
}
?>
Upvotes: 0
Reputation: 1220
After some playing I came up with this solution that works...
$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',
);
Upvotes: 0
Reputation: 1012
Assuming that your storing the date as a timestamp you can add a meta_compare
parameter to your query...
<?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'
);
Give that a go
Dan
Upvotes: 1
Reputation: 328
This is how it should be
<?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;
?>
Upvotes: 1