我有一个自定义的帖子类型,我试图限制搜索结果。
CPT是一个交易/优惠券,每个交易都有一个开始和结束日期。
我只是想在搜索中显示这些帖子:
对于非经常性事件。。。。显示结束日期为今天或今天之后的帖子
OR
<对于经常性事件,将其限制为仅显示结束日期介于今天之间且不超过7天的事件这些查询中的每一个都能独立完美地工作,但我无法将它们组合在一起。。。
function search_pre_get_posts( $query ) {
if ( $query->is_search() && $query->is_main_query() ) {
$recurring_count = am_get_recurring_count($post->ID);
// Show non-recurring events with end date of today or after
if ($recurring_count == 0){
$currentdate = date(\'Y-m-d\');
$query->set(
\'meta_query\',
array(
\'relation\' => \'OR\',
array(
\'key\' => \'am_enddate\',
\'compare\' => \'>=\',
\'value\' => $currentdate,
),
array(
\'key\' => \'am_enddate\',
\'compare\' => \'NOT EXISTS\',
)
)
);
$query->set( \'orderby\', \'meta_value\' );
$query->set( \'order\', \'ASC\' );
} else {
// Show recurring events with end date of today or after, that doesnt exceed 7 days.
$currentdate = date(\'Y-m-d\');
$enddate = strtotime(date(\'Y-m-d\') . "+7 days");
$enddate = date(\'Y-m-d\',$enddate);
$query->set(
\'meta_query\',
array(
\'relation\' => \'OR\',
array(
\'key\' => \'am_enddate\',
\'compare\' => \'NOT EXISTS\',
),
array(
\'relation\' => \'AND\',
array(
\'key\' => \'am_enddate\',
\'compare\' => \'>=\',
\'value\' => $currentdate,
),
array(
\'key\' => \'am_enddate\',
\'compare\' => \'<=\',
\'value\' => $enddate,
)
)
)
);
$query->set( \'orderby\', \'meta_value\' );
$query->set( \'order\', \'ASC\' );
}
}
}
add_action( \'pre_get_posts\', \'search_pre_get_posts\' );
SO网友:Zachary Eagle
删除if语句,并将查询组合成如下内容如何:
\'meta_query\',
array(
\'relation\' => \'OR\',
array(
array(
\'relation\' => \'AND\',
array(
\'key\' => \'am_get_recurring_count\',
\'compare\' => \'=\',
\'value\' => \'0\',
),
array(
\'relation\' => \'OR\',
array(
\'key\' => \'am_enddate\',
\'compare\' => \'>=\',
\'value\' => $currentdate,
),
array(
\'key\' => \'am_enddate\',
\'compare\' => \'NOT EXISTS\',
)
)
)
)
array(
array(
\'relation\' => \'AND\',
array(
\'key\' => \'am_get_recurring_count\',
\'compare\' => \'!=\',
\'value\' => \'0\',
),
array(
\'relation\' => \'OR\',
array(
\'key\' => \'am_enddate\',
\'compare\' => \'NOT EXISTS\',
),
array(
\'relation\' => \'AND\',
array(
\'key\' => \'am_enddate\',
\'compare\' => \'>=\',
\'value\' => $currentdate,
),
array(
\'key\' => \'am_enddate\',
\'compare\' => \'<=\',
\'value\' => $enddate,
)
)
)
)
)
)
还没有机会测试这个。请让我知道它是否有效。
SO网友:Sofiane Achouba
Try this
function search_pre_get_posts( $query ) {
if ( $query->is_search() && $query->is_main_query() ) {
$currentdate = date(\'Y-m-d\');
$enddate = strtotime(date(\'Y-m-d\') . "+7 days");
$enddate = date(\'Y-m-d\',$enddate);
$query->set(
\'meta_query\',
array(
\'relation\' => \'OR\',
array(
\'key\' => \'am_enddate\',
\'compare\' => \'NOT EXISTS\',
),
array(
\'relation\' => \'OR\',
array(
\'key\' => \'am_enddate\',
\'compare\' => \'>=\',
\'value\' => $currentdate,
),
array(
\'key\' => \'am_recurrence_id\',
\'compare\' => \'NOT EXISTS\',
)
),
array(
\'relation\' => \'AND\',
array(
\'key\' => \'am_enddate\',
\'compare\' => \'>=\',
\'value\' => $currentdate,
),
array(
\'key\' => \'am_enddate\',
\'compare\' => \'<=\',
\'value\' => $enddate,
),
array(
\'key\' => \'am_recurrence_id\',
\'compare\' => \'EXISTS\',
)
)
)
);
$query->set( \'orderby\', \'meta_value\' );
$query->set( \'order\', \'ASC\' );
}
}
add_action( \'pre_get_posts\', \'search_pre_get_posts\' );