我正在为活动日历创建自定义帖子。我的自定义帖子有两个自定义元字段,称为“event\\u star\\u date”&;“event\\u end\\u date”。我想使用pre_get_posts
排除其“event\\u end\\u date”小于todays date或“event\\u end\\u date”为空且“event\\u start\\u date”小于todays date的事件。我看到了一些代码,但最终对我不起作用。
多亏了@cybnet,我已经很快得到了我需要的代码,但钢铁无法工作。我想nedd可能是这样的:
function bbwp_calendar_visible_events( $query ) {
if ( !is_admin() && $query->is_main_query() ){
if ( is_post_type_archive( $postType ) ) {
$meta_query = array(
\'relation\' => \'OR\',
array(
\'relation\' => \'AND\',
array(
\'key\' => \'event_start_date\',
\'value\' => current_time( \'timestamp\' ),
\'type\' => \'CHAR\',
\'compare\' => \'>=\',
),
array(
\'key\' => \'event_end_date\',
\'value\' => \'\',
\'type\' => \'CHAR\',
\'compare\' => \'=\',
),
),
array(
\'key\' => \'event_end_date\',
\'value\' => current_time( \'timestamp\' ),
\'type\' => \'CHAR\',
\'compare\' => \'>=\',
),
);
$query->set( \'meta_query\', $meta_query );
}
}
}
add_action( \'pre_get_posts\', \'bbwp_calendar_visible_events\' );
感谢每一个人
SO网友:cybmeta
您可以使用查询的“meta\\u query”参数,我认为这比过滤post_where
:
add_action( \'pre_get_posts\', \'bbwp_calendar_visible_events\' );
function bbwp_calendar_visible_events( $query ) {
$postType = \'bbwp_calendar\';
if ( !is_admin() && $query->is_main_query() ){
if ( is_post_type_archive( $postType ) ) {
$meta_query = array(
\'relation\' => \'OR\',
//current time <= event_end_date
array(
\'key\' => \'event_end_date\',
//Check https://codex.wordpress.org/Function_Reference/current_time
//to return the current time (today) in the same format
//you store the date in your custom field
\'value\' => current_time( \'timestamp\' ),
\'type\' => \'DATETIME\',
\'compare\' => \'<=\',
),
//event_end_date = \'\'
array(
\'key\' => \'event_end_date\',
//Check https://codex.wordpress.org/Function_Reference/current_time
//to return the current time (today) in the same format
//you store the date in your custom field
\'value\' => "",
\'type\' => \'DATETIME\',
\'compare\' => \'=\',
),
);
$query->set( \'meta_query\', $meta_query );
}
}
}
更多信息: