你好@Spencer B.:
有趣的是,我的客户提交了一张前几天我为这个问题编写的事件模块的错误通知单,我几个小时前刚刚修复了它。
请注意my example uses a Custom Post Type of \'event\'
这对于区分不同于POST的事件的逻辑非常有用。If you must use regular Posts 您需要以某种方式确定所需的逻辑,是什么使列出帖子的页面与列出事件的页面不同。
你需要use the \'posts_where\'
hook to filter out the Events whose dates are earlier than 24 hours ago. 下面的钩子函数测试查询是否用于post_type=\'event\'
; 如果是这样,它会修改查询以向SQL添加条件WHERE
条款
保存WordPress时checks to see if it is a future date, and if so sets the post_status=\'future\'
rather than \'publish\'
; you need to correct that. 您可以使用\'wp_insert_post_data\'
要重置的挂钩\'publish\'
如果WordPress已设置为\'future\'
.
下面是一个封装此逻辑的类,您可以将其复制到主题的functions.php
文件:
class Display_Future_Events {
static function on_load() {
add_filter(\'posts_where\',array(__CLASS__,\'posts_where\'),10,2);
add_action(\'wp_insert_post_data\', array(__CLASS__,\'wp_insert_post_data\'),10,2);
add_action(\'init\', array(__CLASS__,\'init\'));
}
static function posts_where($where,$query) {
if (self::is_event_list($query)) {
global $wpdb;
$yesterday = date(\'Y-m-d H:i:s\',time()-(24*60*60));
$where .= $wpdb->prepare(" AND post_date>\'%s\' ",$yesterday);
}
return $where;
}
static function is_event_list($query) {
// Logic here might need to be fine-tuned for your use-case
if (is_string($query->query))
parse_str($query->query,$args);
else
$args = $query->query;
return isset($args[\'post_type\'])==\'event\';
}
static function wp_insert_post_data($data,$postarr) {
if ($data[\'post_type\']==\'event\' && // Will need more logic here for post_type=\'post\'
$postarr[\'post_status\']==\'publish\' &&
$data[\'post_status\']==\'future\')
$data[\'post_status\'] = \'publish\';
return $data;
}
static function init() {
register_post_type(\'event\',
array(
\'labels\' => self::make_labels(\'Event\'),
\'public\' => true,
\'show_ui\' => true,
\'query_var\' => \'event\',
\'rewrite\' => array(\'slug\' => \'events\'),
\'hierarchical\' => true,
\'supports\' => array(\'title\',\'editor\',\'custom-fields\'),
/*
See more \'supports\' options at
http://codex.wordpress.org/Function_Reference/register_post_type
*/
)
);
}
static function make_labels($singular,$plural=false,$args=array()) {
if ($plural===false)
$plural = $singular . \'s\';
elseif ($plural===true)
$plural = $singular;
$defaults = array(
\'name\' =>_x($plural,\'post type general name\'),
\'singular_name\' =>_x($singular,\'post type singular name\'),
\'add_new\' =>_x(\'Add New\',$singular),
\'add_new_item\' =>__("Add New $singular"),
\'edit_item\' =>__("Edit $singular"),
\'new_item\' =>__("New $singular"),
\'view_item\' =>__("View $singular"),
\'search_items\' =>__("Search $plural"),
\'not_found\' =>__("No $plural Found"),
\'not_found_in_trash\' =>__("No $plural Found in Trash"),
\'parent_item_colon\' =>\'\',
);
return wp_parse_args($args,$defaults);
}
}
Display_Future_Events::on_load();