呈现出一定的日期范围之间的员额 [英] Showing posts between a certain date range
问题描述
试图显示我的自定义文章类型为特定日期范围。我想只在某个月显示帖子。我知道我需要挂接到posts_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.
我已经看到了很多关于如何改变WHERE子句采取的日期范围的例子,但只有当静态的。我需要做到以下几点:
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;
}
希望是有道理的。任何帮助将是AP preciated。
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屋!