I currently have an events section with a heading and upcoming events below that. The events hide after their date passes but when there are no events displayed the heading is still visible. I want the whole section to not be visible when there are no posts but because the WP_Query still recognizes that there are posts the heading is visible. Can you include the is statement to check the event date against the current date with the WP_Qurey?
<?php
$current_date = date('F j, Y');
$current_date = strtotime( $current_date );
$args = array(
'post_type' => 'event',
'showposts' => 4,
'meta_key'=>'event_date',
'orderby' => 'meta_value',
'order' => 'ASC',
);
$events_query = new WP_Query( $args );
?>
<?php if ( $events_query->have_posts() ) : ?>
<h2>Upcoming Events</h2>
<?php while ( $events_query->have_posts() ) : $events_query->the_post(); ?>
<?php
$event_date = get_field('event_date');
$event_date = strtotime( $event_date );
if( $event_date > $current_date ):
?>
<h3><?php the_title(); ?></h3>
<a href="<?php the_permalink() ?>">View</a>
<?php endif; ?>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
Sawyer is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.