Skip to content

Instantly share code, notes, and snippets.

@mattboon
Created March 12, 2012 10:45
Show Gist options
  • Save mattboon/2021115 to your computer and use it in GitHub Desktop.
Save mattboon/2021115 to your computer and use it in GitHub Desktop.
WordPress - Query Posts with date custom field, hide those in past
<ul class="feed events">
<?php
global $post;
$args = array(
'numberposts' => 3,
'category' => 3,
'meta_key' => 'event_date',
'meta_value' => date("Y-m-d"),
'meta_compare' => '>=',
'orderby' => 'meta_value',
'order' => 'ASC'
);
$myposts = get_posts( $args );
foreach($myposts as $post) : setup_postdata($post);
$event_location = get_post_meta($post->ID, 'event_location', true);
$event_date = get_post_meta($post->ID, 'event_date', true);
?>
<li<?= has_post_thumbnail() ? ' class="img"' : '' ?>>
<?php if(has_post_thumbnail()): ?>
<a href="<?php the_permalink(); ?>"><?php the_post_thumbnail('feed-thumb', array('alt' => get_the_title(), 'title' => '' )); ?></a>
<?php endif; ?>
<h2>
<a href="<?php the_permalink(); ?>">
<?php the_title(); ?>
</a>
</h2>
<?php the_excerpt(); ?>
<?php if($event_location || $event_date): ?>
<ul class="info">
<?= $event_date ? '<li class="icon date">' . $event_date . '</li>' : '' ?>
<?= $event_location ? '<li class="icon map">' . $event_location . '</li>' : '' ?>
</ul>
<?php endif; ?>
</li>
<?php endforeach; ?>
</ul>
<?php wp_reset_query(); ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment