dragon19720808 2015-03-10 17:49
浏览 18
已采纳

Wordpress自定义帖子类型 - 仅列出UPCOMING事件 - 不是所有事件

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();        
    ?>
  • 写回答

4条回答 默认 最新

  • douye6812 2015-03-12 12:45
    关注

    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',
    );
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?