Home › Forums › Back End › Using WP's pre_get_posts to exclude post with meta_key › Reply To: Using WP's pre_get_posts to exclude post with meta_key
January 18, 2017 at 9:46 am
#250176
Kevin deLeon
Participant
I know this post is over a year old…but I thought I’d chime in. Your final solution would break any other meta_queries using “post__not_in” if you happened to have any…I ran into this problem on my site.
So it may be better to preserve that by doing something like:
function exclude_best_of_show( $query ) {
if ( !is_admin() && $query->is_main_query() && is_post_type_archive( 'shows' ) ) {
// Get any post__not_in arrays already set
$current_not_in_array = $query->get('post__not_in');
// Merge them with your new array to exclude
$new_post_not_in = array_merge($current_not_in_array, [680]);
$query->set( 'post__not_in', $new_post_not_in );
}
}
add_action( 'pre_get_posts', 'exclude_best_of_show' );