In the sidebar of this page: http://lifebridgecypress.org/our-people, I have a list of upcoming events using this code…
<ul id="upcoming-events">
<?php
$latestPosts = new WP_Query();
$latestPosts->query('cat=3&showposts=10');
?>
<?php while ($latestPosts->have_posts()) : $latestPosts->the_post(); ?>
<li><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></li>
<?php endwhile; ?>
</ul>
The only problem is that these events have already past… ha ha. I’d like to configure the code where it only shows upcoming events in the future, and then 24 hours after the event, it would disappear from this upcoming events list in the sidebar.
Does anyone know how to do this by modifying this code?
Hi @Spencer B.:
Funny, my client submitted a bug ticket for the events modules I wrote for this very issue the other day, and I just fixed it a few hours ago.
Note that my example uses a Custom Post Type of
'event'
which is very useful to be able to differentiate logic for Events distinct from Posts. If you must use regular Posts you’ll need to somehow identify the required logic what makes the pages that list Posts different from your page that lists Events.You’ll need to use the
'posts_where'
hook to filter out the Events whose dates are earlier than 24 hours ago. The hook function below tests to see if the query is forpost_type='event'
; if so it modifies the query to add a criteria to the SQLWHERE
clause.When you save a WordPress checks to see if it is a future date, and if so sets the
post_status='future'
rather than'publish'
; you need to correct that. You can use the'wp_insert_post_data'
hook to reset to'publish'
if WordPress has set to'future'
.What follows is a class to encapsulate this logic, which you can copy into your theme’s
functions.php
file:This code will only show your latest posts. One solution is to unpublish the posts regarding events that have already past.