Skip to content

Instantly share code, notes, and snippets.

@searchwpgists
Created March 29, 2022 15:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save searchwpgists/c5a651a4a7ec75ffd1b0f65429d89d4d to your computer and use it in GitHub Desktop.
Save searchwpgists/c5a651a4a7ec75ffd1b0f65429d89d4d to your computer and use it in GitHub Desktop.
Tell SearchWP to ignore any post(s) with a specified meta key value
<?php
// Tell SearchWP to exclude any posts with a my_meta_key
// value of 'meta value 1', 'meta value 2', or 'meta value 3'.
add_filter( 'searchwp\post__not_in', function( $ids ) {
return array_unique( array_merge( $ids, get_posts( [
'fields' => 'ids',
'nopaging' => true,
'post_type' => 'any',
'meta_query' => [ [
'key' => 'my_meta_key',
'value' => [ 'meta value 1', 'meta value 2', 'meta value 3', ],
'compare' => 'IN',
], ],
] ) ) );
}, 20, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment