So I spent like 5 hours trying to figure out why my proper-looking code wasn’t giving me the behavior I wanted. Thanks to the fine commentators I was able to get current posts to display without the event_date specified but wasn’t able to fetch posts by date.
I started thinking outside of the box and learned that it was showing ‘todays’ events but the timezone of the server was in a different country so it was tomorrow’s events for me. I specified the default timezone and it works perfectly!
Here’s the shortcode I made to fetch my custom post type ‘event’ according to which ones have the custom field ‘event_date’ value of todays date.
// Add Shortcode
function custom_shortcode() {
date_default_timezone_set("America/New_York");
// find todays date
$today = date("Ymd");
// args
$args = array(
'post_type' => 'event',
'meta_key' => 'event_date',
'meta_value' => $today
);
// query
$the_query = new WP_Query( $args );
// The Loop
if ( $the_query->have_posts() ) {
$out .= '<ul>';
while ( $the_query->have_posts() ) {
$the_query->the_post();
$out .= "<div>";
$out .= get_field('event_name') . "<br>";
$out .='<img class="myimage" style="max-width:180px;" src=""https://wordpress.stackexchange.com/questions/248228/. get_field("event_image') . '">' ;
$out .= " <br>";
$out .= '<div class="date">"https://wordpress.stackexchange.com/questions/248228/. get_field("event_date') . "</div>" . "<br>";
$out .= '<div class="time">"https://wordpress.stackexchange.com/questions/248228/. get_field("event_time') . "</div>" ;
$out .= get_field('buy_ticket_button') . "<br>";
$out .= "</div>";
}
wp_reset_postdata();
}
return $out;
}
add_shortcode( 'todaysevent', 'custom_shortcode' );