(Wordpress) Only show future posts in archive & search results
I have an events category which is searchable by a sub-category (by city where the event is being held). I have the 'The Future is Now!' plugin to allow me to publish future dated posts but hoping that I can set up a paginated templ开发者_C百科ate that, when a post 'expires' it will no longer show up in the loop/archive.
I'm also wondering if you can filter out these posts from search results as well?
This is my current Events page code if this helps:
<h2>Upcoming Events</h2>
<ul class="posts">
<?php
$limit = get_option('posts_per_page');
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
query_posts('showposts=' . $limit . '&paged=' . $paged .'&cat=1&order=ASC');
$wp_query->is_archive = true; $wp_query->is_home = false;
?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<li class="events_ticker" id="post-<?php the_ID(); ?>">
<span class="left">
<h3><?php the_time('F jS, Y') ?></h3>
<p><?php if (strlen($post->post_title) > 25) { echo substr(the_title($before = '', $after = '', FALSE), 0, 25) . '...';} else {the_title();} ?></p>
<?php global $more; $more = 0; the_excerpt(); ?>
<p><a href="<?php echo get_permalink(); ?>">Read More</a></p>
</span>
<a href="<?php echo get_permalink(); ?>"><?php if ( function_exists("has_post_thumbnail") && has_post_thumbnail() ) { the_post_thumbnail(array(143,110), array("class" => "right post_thumbnail")); } ?></a>
</li>
<img src="<?php bloginfo('stylesheet_directory'); ?>/images/content_breaker_wide.png" alt=" breaker" class="content_breaker" />
<?php endwhile; ?>
</ul>
<!-- end events loop -->
<div class="navigation">
<p class="alignleft"><?php next_posts_link('< Next') ?></p>
<p class="alignright"><?php previous_posts_link('Next >') ?></p>
</div>
<?php endif; ?>
I don't think there is an easy parameter to say "all posts until this date", so you should implement the posts_where_paged
filter. There, you can check for !(is_archive() || is_search())
, because these two can continue as normal. For the other parts, you add a " AND $wpdb->posts.post_date < NOW()"
or something like that (but don't use now, since that will hide events that happen later today, and you probably don't want that).
A similar question was asked on the WordPress Answers Stack Exchange site (in private beta until Aug 20 2010, so you can't visit it unless you pre-registered). Joe Hoyle's suggestion there was simple:
If all you are wanting to do is add an extra date for 'show times', it may be easier to add a meta box which does exactly that - using the Publish date to spoof it could be potentially problematic, as WordPress will set it to "future" status whenever it is updated with a future publish date (I think), so you will have to be hooking every time a post is updated to set it back again. Also, that way you could reserve "Publish Date" for what it is intended.
I would probably just use a
meta_key
, and a custom meta box. Though it depends how complex your queries are that show the posts.If you set the meta_value as a timestamp, you can order by the show time date still, or select before / after a certain date:
$events = get_posts('post_type=events&meta_key=show_time&meta_value=' . strtotime( '+1 week' ) . '&meta_compare=<&orderby=meta_value');
Would get all "events" with a showtime no later then a week from the current date. Note: The above is untested, but should work :)
(This answer is community wiki so I don't get rep points for just reposting what Joe said.)
精彩评论