I am trying to retrieve the IDs of posts with a certain meta value (this works fine) I then try to pass them through post__not_in and it does not exclude the posts from the wordpress search.
I have the integer array (from a var_dump):
array(2) { [0]=> int(373) [1]=> int(247) }
However, I now need to convert that array into 373,247 for use in post__not_in. Any ideas?
remove_action('pre_get_posts','exclude_pages_from_search');
$hidePages = new WP_Query( array (
'post_type' => array( 'post', 'page', 'offer', 'review', 'project' ),
'ignore_sticky_posts' => true,
'posts_per_page' => -1,
'meta_key' => 'edit_screen_sitemap',
'meta_value' => 'hide',
'fields' => 'ids'
));
$test = $hidePages->posts;
function exclude_pages_from_search($query) {
if ( !is_admin() ) {
if ( $query->is_main_query() ) {
if ($query->is_search) {
$query->set('post__not_in', $test);
}
}
}
} add_action('pre_get_posts','exclude_pages_from_search');
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…