显示特定日期范围之间的帖子 [英] Showing posts between a certain date range

查看:32
本文介绍了显示特定日期范围之间的帖子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试显示特定日期范围的自定义帖子类型.我只想显示某个月内的帖子.我知道我需要连接到 posts_where 过滤器,但我不知道如何将参数传递给这个函数,因为我需要传递日期范围.

我已经看到很多关于如何更改 WHERE 子句以采用日期范围的示例,但仅限于静态时.我需要执行以下操作:

add_filter('posts_where', 'my_custom_where', '', '04/2011');//将我的日期传递给过滤器函数 my_custom_where( $where = '' ) {//数字范围$范围=数组('开始' =>$日期.'-1','结束' =>$日期.'-' .cal_days_in_month(CAL_GREGORIAN, date('m', $date), date('Y', $date)));$where .= " AND post_date .....";//构建日期范围的位置返回 $where;}

希望这是有道理的.任何帮助将不胜感激.

解决方案

如果你使用全局变量,你可以让它变得动态.(不理想,但是嘿,我还没有找到更干净的方法......)

首先为日期定义一个全局变量

$GLOBALS['start_date'] = '2011-07-31';

然后添加过滤器

add_filter('posts_where', 'filter_where');函数 filter_where( $date, $where = '' ) {//晚于动态日期的帖子$where .= " AND post_date >='" .date('Y-m-d H:i:s', strtotime($GLOBALS['start_date'])) ."'";返回 $where;}

如果您只想为单个查询运行过滤器,请删除过滤器.

Trying to display my custom post types for specific date ranges. I want to show posts only within a certain month. I know I need to hook into the posts_where filter, but I can not figure out how to pass arguments to this function, as I need to pass the date range.

I have seen plenty of examples of how to alter the WHERE clause to take a date range, but only when static. I need to do the following:

add_filter('posts_where', 'my_custom_where', '', '04/2011'); //pass my date to the filter

function my_custom_where( $where = '' ) {

    //figure range  
    $range = array(
        'start' => $date . '-1',
        'end' => $date . '-' . cal_days_in_month(CAL_GREGORIAN, date('m', $date), date('Y', $date))
    );

    $where .= " AND post_date ....."; //build where with date range

    return $where;

}

Hope that makes sense. Any help would be appreciated.

解决方案

You can make it dynamic if you resort to global variables.(Not ideal, but hey, I haven't found a cleaner way...)

First define a global variable for the date

$GLOBALS['start_date'] = '2011-07-31';

then add your filter

add_filter( 'posts_where', 'filter_where' );

 function filter_where( $date, $where = '' ) {
    // posts later than dynamic date
    $where .= " AND post_date >= '" . date('Y-m-d H:i:s', strtotime($GLOBALS['start_date'])) . "'";
    return $where;
}

Then remove the filter if you just want to run that for a single query.

这篇关于显示特定日期范围之间的帖子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆