Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Apply Exclude from Search Filter on ES Query #3266

Merged
merged 1 commit into from
Jan 20, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 18 additions & 34 deletions includes/classes/Feature/Search/Search.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public function search_setup() {

add_action( 'init', [ $this, 'register_meta' ], 20 );
add_action( 'enqueue_block_editor_assets', [ $this, 'enqueue_block_editor_assets' ] );
add_action( 'pre_get_posts', [ $this, 'exclude_posts_from_search' ] );
add_filter( 'ep_post_filters', [ $this, 'exclude_posts_from_search' ], 10, 3 );
add_action( 'post_submitbox_misc_actions', [ $this, 'output_exclude_from_search_setting' ] );
add_action( 'edit_post', [ $this, 'save_exclude_from_search_meta' ], 10, 2 );
}
Expand Down Expand Up @@ -707,54 +707,38 @@ public function enqueue_block_editor_assets() {
/**
* Exclude posts based on ep_exclude_from_search post meta.
*
* @param WP_Query $query WP Query
* @param array $filters Filters to be applied to the query
* @param array $args WP Query args
* @param WP_Query $query WP Query object
*/
public function exclude_posts_from_search( $query ) {
public function exclude_posts_from_search( $filters, $args, $query ) {
$bypass_exclusion_from_search = is_admin() || ! $query->is_search();
/**
* Filter whether the exclusion from the "exclude from search" checkbox should be applied
*
* @since 4.4.0
* @hook ep_bypass_exclusion_from_search
* @param {bool} $bypass_exclusion_from_search True means all posts will be returned
* @param {WP_Query} $query WP Query
* @param {bool} $bypass_exclusion_from_search True means all posts will be returned
* @param {WP_Query} $query WP Query
* @return {bool} New $bypass_exclusion_from_search value
*/
if ( apply_filters( 'ep_bypass_exclusion_from_search', $bypass_exclusion_from_search, $query ) ) {
return;
return $filters;
}

// Get any meta query that's being added before.
$meta_query = (array) $query->get( 'meta_query' );

$exclude_from_search_query = [
'relation' => 'or',
[
'key' => 'ep_exclude_from_search',
'compare' => 'NOT EXISTS',
],
[
'key' => 'ep_exclude_from_search',
'value' => '1',
'compare' => '!=',
$filters[] = [
'bool' => [
'must_not' => [
[
'terms' => [
'meta.ep_exclude_from_search.raw' => [ '1' ],
],
],
],
],
];

/**
* If the current meta query only has an `OR` clause, wrap it with an `AND`
* so the criteria here is not made "optional".
*/
if ( empty( $meta_query ) || empty( $meta_query['relation'] ) || 'and' === strtolower( $meta_query['relation'] ) ) {
$meta_query[] = $exclude_from_search_query;
} else {
$meta_query = [
'relation' => 'and',
$exclude_from_search_query,
$meta_query,
];
}

$query->set( 'meta_query', $meta_query );
return $filters;
}

/**
Expand Down