Skip to content

Instantly share code, notes, and snippets.

@DumahX
Last active August 15, 2022 16:47
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 DumahX/b12f64a7ecec4e8c2024930f958f8b6f to your computer and use it in GitHub Desktop.
Save DumahX/b12f64a7ecec4e8c2024930f958f8b6f to your computer and use it in GitHub Desktop.
Excludes protected posts from WP search results
<?php
function maybe_exclude_protected_posts($query) {
if(!$query->is_admin && $query->is_search && $query->is_main_query()) {
$posts_to_exclude = array();
$posts = get_posts(array('numberposts' => -1));
foreach($posts as $post) {
if(MeprRule::is_locked($post)) {
$posts_to_exclude[] = $post->ID;
}
}
if(!empty($posts_to_exclude)) {
$query->set('post__not_in', $posts_to_exclude);
}
}
}
add_action('pre_get_posts', 'maybe_exclude_protected_posts');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment