【发布时间】:2018-10-22 15:26:30
【问题描述】:
我正在尝试根据 WP_Query 检索要从搜索查询中删除的 ID 列表。无论出于何种原因,即使我知道帖子 id 373 具有正确的查询条件,WP_Query 也没有显示 ID 数组。
remove_action('pre_get_posts','exclude_pages_from_search');
$hidePages = new WP_Query( array (
'meta_key' => 'edit_screen_sitemap',
'meta_value' => 'hide',
'fields' => 'ids'
));
$hidePageIds = array($hidePages->posts);
$hidePageIdss = array($hidePages);
var_dump($hidePageIds); // array(1) { [0]=> array(0) { } }
var_dump($hidePageIdss); // displays query array
add_action('pre_get_posts','exclude_pages_from_search');
function exclude_pages_from_search($query) {
if ( !is_admin() ) {
if ( $query->is_main_query() ) {
if ($query->is_search) {
$query->set('post__not_in', array($hidePages->posts));
}
}
}
}
【问题讨论】: