douza6300 2012-02-16 18:50 采纳率: 0%
浏览 39
已采纳

Wordpress:仅显示未来的帖子减去一天

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; ?>
  • 写回答

2条回答 默认 最新

  • dqkxo44488 2012-02-16 19:26
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题
  • ¥15 企业资源规划ERP沙盘模拟