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');
The issue I was orginally having was I was imploding the array and turning it into a string when I needed an integer array. So I removed the imploding of the Query and left it as a variable which of course became an integer array as post___not_in only allows integer arrays.
I then ran into the trouble of a memory leak if I ran the query inside the function so I had to figure out how to run it outside the function and still be able to use the variable inside the function as it would be undefined.
With the addition of global $hidePageIds, I was able to access the variable holding the integer array inside the function and therefore pass it in the post__not_in query.