Skip to content

Instantly share code, notes, and snippets.

@FreshLondon
Created March 5, 2019 18:18
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 FreshLondon/3b7213ea20b2daa21b3ee14de383e1fa to your computer and use it in GitHub Desktop.
Save FreshLondon/3b7213ea20b2daa21b3ee14de383e1fa to your computer and use it in GitHub Desktop.
Strips posts from search queries if ACF meta value post_is_hidden is active
<?
/**
* Strips posts from search queries if ACF meta value post_is_hidden is active.
*
* @author AM.HIGH
* @version 1.0.0
* @since 1.0.0
*/
function dontSearchTheHidden($query) {
if (!$query->is_search()):
return;
endif;
$query->set('meta_query', array(
'relation' => 'OR',
array(
'key' => 'post_is_hidden',
'compare' => 'NOT EXISTS',
),
array(
'key' => 'post_is_hidden',
'value' => '0',
'compare' => '=',
),
));
$query->set('post_type',array('post','page','property'));
}
add_action('pre_get_posts', 'dontSearchTheHidden');
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment