php - WordPress - Hiding dates in past with get_posts and Advanced Custom Fields -
i have page show 3 upcoming events. getting 3 posts specific parent, ordered date set using date picker advanced custom fields plugin.
i have managed work, shows right posts, ordered based on date picker. see code below:
<?php $posts = get_posts(array( 'post_type' => 'page', 'post_parent' => 307, 'numberposts' => 3, 'meta_key' => 'info_startdate', 'orderby' => 'meta_value_num', 'order' => 'asc' )); ?> <?php foreach($posts $post) { ?> <?php setup_postdata($post); ?> <div class="col-md-4"> <?php $info_date = get_field("info_date"); ?> <small><?php echo $info_date; ?></small> <h5><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h5> </div> <?php } ?> <?php wp_reset_postdata(); ?>
now comes problem, need not show result if event has passed. if date picker's value date in past. found code did trick, see below:
<?php foreach($posts $post) { ?> <?php setup_postdata($post); ?> <?php $end_date_passed_check = datetime::createfromformat('ymd', get_field('info_startdate')); if ($end_date_passed_check->format('ymd') < date('ymd')) { // nothing } else { ?> <div class="col-md-4"> <?php $info_date = get_field("info_date"); ?> <small><?php echo $info_date; ?></small> <h5><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h5> </div> <?php } ?>
now doesn't show results in past, great. because get_posts function limiting results 3 'numberpost', if there example 2 posts in past, show 1 post. how make not show posts in past, still show 3 results?
Comments
Post a Comment