Skip to content

Instantly share code, notes, and snippets.

@brycejacobson
Created December 4, 2013 21:17
Show Gist options
  • Save brycejacobson/7795733 to your computer and use it in GitHub Desktop.
Save brycejacobson/7795733 to your computer and use it in GitHub Desktop.
Get custom field date in WP_Query and show the post if the date is in the future. Using Advance Custom Fields.
<?php
$today = date('Ymd', strtotime('-1 day'));
// WP_Query arguments
$args = array (
'post_type' => 'construction_bids',
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => 'bid_deadline',
'value' => $today,
'compare' => '>',
),
),
);
// the query
$query = new WP_Query( $args ); ?>
<?php if ( $query->have_posts() ) : ?>
<!-- pagination here -->
<!-- the loop -->
<?php while ( $query->have_posts() ) : $query->the_post(); $date = DateTime::createFromFormat('Ymd', get_field('bid_deadline')); ?>
<h2><a href="<? echo get_permalink(); ?>"><?php echo get_the_title(); ?></a></h2>
<p><strong>Bid Deadline:</strong> 3:00 PM, <?php echo $date->format('F j Y'); ?></p>
<h3>Summary:</h3>
<?php echo the_field('summary'); ?>
<?php endwhile; ?>
<!-- end of the loop -->
<!-- pagination here -->
<?php wp_reset_postdata(); ?>
<?php else: ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>
@apprecht
Copy link

apprecht commented Sep 6, 2019

Thanks a lot, saved me some time :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment