我创建了一个自定义帖子类型来显示未来的事件,事件日期存储在自定义字段中(YYYY/MM/DD格式)。我希望此自定义帖子类型的管理屏幕按事件日期自定义字段自动排序。
试过MikeSchnickel的tutorial 在创建“排序依据”列时,我想我已经解决了需要使用类似于他使用的parse\\u查询过滤器的东西。但我似乎不能适应我的需要,
我对这样定制wordpress还很陌生,所以非常感谢一些简单的指导!
以下是我的代码,用于注册自定义类型的post(gs\\U事件),为自定义字段添加元框(event\\U date),并为admin屏幕设置自定义列。。。。
add_action(\'init\', \'my_custom_init\');
function my_custom_init() {
$labels = array(
\'name\' => _x(\'Events\', \'post type general name\'),
\'singular_name\' => _x(\'Event\', \'post type singular name\'),
\'add_new\' => _x(\'Add New\', \'event\'),
\'add_new_item\' => __(\'Add New Event\'),
\'edit_item\' => __(\'Edit Event\'),
\'new_item\' => __(\'New Event\'),
\'view_item\' => __(\'View Event\'),
\'search_items\' => __(\'Search Events\'),
\'not_found\' => __(\'No events found\'),
\'not_found_in_trash\' => __(\'No events found in Trash\'),
\'parent_item_colon\' => \'\'
);
$args = array(
\'labels\' => $labels,
\'public\' => true,
\'publicly_queryable\' => true,
\'show_ui\' => true,
\'query_var\' => true,
\'rewrite\' => array(\'slug\' => \'events\'),
\'capability_type\' => \'post\',
\'hierarchical\' => false,
\'menu_position\' => 5,
\'supports\' => array(\'title\',\'editor\',\'thumbnail\',\'comments\')
);
register_post_type(\'gs_events\',$args);
}
register_taxonomy("Location", array("gs_events"), array("hierarchical" => true, "label" => "Cities", "singular_label" => "City", "rewrite" => true));
add_action("admin_init", "admin_init");
function admin_init(){
add_meta_box("event_date-meta", "Event Date", "event_date", "gs_events", "side", "high");
}
function event_date(){
global $post;
$custom = get_post_custom($post->ID);
$event_date = $custom["event_date"][0];
?>
<label>Date:</label>
<input name="event_date" value="<?php echo $event_date; ?>" />
<?php
}
add_action(\'save_post\', \'save_details\');
function save_details(){
global $post;
update_post_meta($post->ID, "event_date", $_POST["event_date"]);
}
add_action("manage_posts_custom_column", "events_custom_columns");
add_filter("manage_edit-gs_events_columns", "events_edit_columns");
function events_edit_columns($columns){
$columns = array(
"cb" => "<input type=\\"checkbox\\" />",
"title" => "Venue",
"location" => "Location",
"event_date" => "Date",
);
return $columns;
}
function events_custom_columns($column){
global $post;
switch ($column) {
case "event_date":
$custom = get_post_custom();
echo $custom["event_date"][0];
break;
case "location":
echo get_the_term_list($post->ID, \'Location\', \'\', \', \',\'\');
break;
}
}
我想我需要对MikeSchnickels前面的教程中的代码进行一些修改,但我不确定在这种情况下如何实现它。。。
add_filter( \'parse_query\', \'sort_movie_by_meta_value\' );
function sort_movie_by_meta_value($query) {
global $pagenow;
if (is_admin() && $pagenow==\'edit.php\' &&
isset($_GET[\'post_type\']) && isset($_GET[\'post_type\'])==\'movie\' &&
isset($_GET[\'sortby\']) && $_GET[\'sortby\'] !=\'None\') {
$query->query_vars[\'orderby\'] = \'meta_value\';
$query->query_vars[\'meta_key\'] = $_GET[\'sortby\'];
}
}
SO网友:Werner
相反,将事件元字段日期保存为unix时间戳,而不是字符串,否则会使您的生活变得太困难。它只是让日期比较变得容易多了。
以下是如何将YYYY/MM/DD格式转换为unix时间戳的示例:
$date = \'2010/09/22\';
$dateParts = explode(\'/\', trim($date));
if (count($dateParts) == 3) {
$timestamp = mktime(0, 0, 0, $dateParts[1], $dateParts[2], $dateParts[0]);
} else {
$timestamp = 0;
// The input value is dodgy, therefore the timestamp is set to 0, which is
// effectively 1970-01-01T00:00:00Z.
}
要再次转换回以供用户编辑和显示,只需执行以下操作:
$displayValue = date(\'Y/m/d\', $timestamp);
我还建议您将自定义字段命名为更具体的名称,如gs\\U event\\U date?
完成后,创建过滤器:
function gs_events_pre_get_posts($query) {
// Note: Use the $pagenow global for finer grained checking,
// if you want to. There are many examples out there.
// Your question pertains to admin use only...
if (is_admin()) {
// Present the posts in your meta_key field\'s order in admin
if (isset($query->query_vars[\'post_type\'])) {
if ($query->query_vars[\'post_type\'] == \'gs_events\') {
$query->set(\'meta_key\', \'gs_event_date\');
$query->set(\'orderby\', \'meta_value\');
$query->set(\'order\', \'ASC\');
// ...and if you only want your future events to be
// displayed in admin, you can uncomment the following:
// $query->set(\'meta_compare\', \'>=\');
// $query->set(\'meta_value\', time());
// Note: The use of time() is quick-and-dirty here,
// in reality you\'ll need to adjust the timezone,
// check server-time vs local time, etc.
}
}
}
}
添加筛选器。。。
add_filter(\'pre_get_posts\' , \'gs_events_pre_get_posts\');