Wordpress: Only show future posts minus one day

2019-07-17 18:09发布

So I have a loop that runs perfect for events and only shows future posts. The issue is that I would love to keep posts that are no longer future posts in the loop for an extra day.

Example: So if the event (or post that is scheduled) is for 8pm on the 3rd. As of now it is removed at 8pm (which is an issue because it might last for 4 hours).

I would like posts to remain up for an extra day, or time that I would be able to alter.

Here is my current code:

<?php
                    $args = array( 'post_type' => 'event', 'posts_per_page' => 50, 'post_status' => 'future', 'order' => 'ASC' );
                    $loop = new WP_Query( $args );
                    if ( have_posts() ) : while ( $loop->have_posts() ) : $loop->the_post();?>
                        <div class="teaser-event <?php the_field('highlight') ?>">
                            <div class="event-meta gold">
                            <div class="event-date"><?php the_time('M d'); ?></div>
                                <div class="event-time"><?php the_time('g:i A'); ?></div>
                            </div>
                            <div class="event-title">
                                <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>">
                                    <?php the_title(); ?>
                                </a>
                            </div>
                        </div>
                        <?php  endwhile; else: ?>
                        <p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
                    <?php endif; ?>

标签: php wordpress
2条回答
小情绪 Triste *
2楼-- · 2019-07-17 18:29

It seems like the time parameters for WP_Query are able to specify definite time spans but not indefinite ones, e.g. posts from now into the future. The WordPress docs recommends using the posts_where filter for time-relative queries. So you might put this in your theme's functions.php:

// Create a new filtering function that will add our where clause to the query
function filter_where($where = '') {
    // posts from yesterday into the future
    $where .= ' AND post_date >= "' . date('Y-m-d', strtotime('-1 day')) . '"';
    return $where;
}

And in your code above you could do:

$args = array('post_type' => 'event', 'posts_per_page' => 50, 'order' => 'ASC');
add_filter('posts_where', 'filter_where');
$loop = new WP_Query($args);
remove_filter('posts_where', 'filter_where');
if ( have_posts() ) : while ( $loop->have_posts() ) : $loop->the_post();

The adding and removing filter doesn't make this the most elegant solution, so you could probably clean it up by defining a custom function get_recent_and_future_posts() in your theme's functions.php that returns whatever object $loop is.

查看更多
一夜七次
3楼-- · 2019-07-17 18:35

I took a look at: http://codex.wordpress.org/Class_Reference/WP_Query

Down the page there's a section called "Time Parameters".

I think instead of looking for post_status in the future you want to look for posts with a date greater than current date - 1 day.

查看更多
登录 后发表回答