添加一个查询变量来存储月份,因此首先需要创建一个自定义查询变量-这将以2012年6月的格式存储我们所处的月份。我会叫它的custom_month
, 但最好的做法是在其前面加前缀,以避免与其他插件发生冲突:
add_filter(\'query_vars\', \'wpse57344_register_query_vars\' );
function wpse57344_register_query_vars( $qvars ){
//Add these query variables
$qvars[] = \'custom_month\';
return $qvars;
}
添加下拉列表现在可以识别查询变量,WordPress在主查询中存储的任何给定值(全局
$wp_query
).
接下来,我们在post admin表的顶部创建下拉列表(可以更改为任何post类型)。
add_action( \'restrict_manage_posts\', \'wpse57344_restrict_posts_by_metavalue\' );
function wpse57344_restrict_posts_by_metavalue() {
global $typenow;
$months = wpse57344_get_months();
if ($typenow == \'post\') {
$selected = get_query_var(\'custom_month\');
$output = "<select style=\'width:150px\' name=\'custom_month\' class=\'postform\'>\\n";
$output .= \'<option \'.selected($selected,0,false).\' value="">\'.__(\'Show All\',\'wpse57344_plugin\').\'</option>\';
if ( ! empty( $months ) ) {
foreach ($months as $month):
$value =esc_attr($month->year.\'/\'.$month->month);
$month_dt = new DateTime($month->year.\'-\'.$month->month.\'-01\');
$output .= "<option value=\'{$value}\' ".selected($selected,$value,false).\'>\'.$month_dt->format(\'F Y\').\'</option>\';
endforeach;
}
$output .= "</select>\\n";
echo $output;
}
}
上面获取了一个月数组。一个月对象包括:
year => //the year of the month e.g. 2012
month => //the number of the month, e.g. 6 for July
posts => //number of posts with the date meta value in this month
显然
wpse57344_get_months()
本机不存在-我们稍后将构建它。
假设有一个月数组,我们创建下拉列表,每个选项都具有表单的值yyyy/mm
. 我已经给了表格same name as the query variable we added.
更改查询
如果选择下拉列表中的月份,则
yyyy/mm
作为的值过帐
custom_month
. 因为我们已经向WordPress注册了这个变量名,所以我们可以通过
$query->get(\'custom_month\')
.
因此,我们检查它是否为空-如果不是,那么我们将限制在其元数据在当月的帖子。为此,我们使用一个元查询和BETWEEN
操作人员
add_action( \'pre_get_posts\', \'wpse57351_pre_get_posts\' );
function wpse57351_pre_get_posts( $query ) {
//Only alter query if custom variable is set.
$month_str = $query->get(\'custom_month\');
if( !empty($month_str) ){
//For debugging, uncomment following line
//var_dump($query);
//Be careful not override any existing meta queries.
$meta_query = $query->get(\'meta_query\');
if( empty($meta_query) )
$meta_query = array();
//Convert 2012/05 into a datetime object get the first and last days of that month in yyyy/mm/dd format
$month = new DateTime($month_str.\'/01\');
//Get posts with date between the first and last of given month
$meta_query[] = array(
\'key\' => \'customdate\',
\'value\' => array($month->format(\'Y/m/d\'),$month->format(\'Y/m/t\')),
\'compare\' => \'BETWEEN\',
);
$query->set(\'meta_query\',$meta_query);
//For debugging, uncomment following line
//var_dump($query);
}
}
获取月份,然后定义函数
wpse57344_get_months()
. 我们需要查询Posteta表,并从您的帖子元中的日期中选择不同的月份。在下文中,我假设您的日期是用“customdate”键存储的,并且具有格式(如注释所示)
yyyy/mm/dd
.
function wpse57344_get_months(){
global $wpdb;
$months = wp_cache_get( \'wpse57344_months\' );
if ( false === $months ) {
$query = "SELECT YEAR(meta_value) AS `year`, MONTH(meta_value) AS `month`, count(post_id) as posts
FROM $wpdb->postmeta WHERE meta_key =\'customdate\'
GROUP BY YEAR(meta_value), MONTH(meta_value) ORDER BY meta_value DESC";
$months = $wpdb->get_results($query);
wp_cache_set( \'wpse57344_months\', $months );
}
return $months;
}
这将返回所需格式的月份对象数组。