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

Make "Exclude from search results" work in AJAX contexts #4025

Merged
merged 1 commit into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
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
3 changes: 2 additions & 1 deletion includes/classes/Feature/Search/Search.php
Original file line number Diff line number Diff line change
Expand Up @@ -738,7 +738,8 @@ public function enqueue_block_editor_assets() {
* @param WP_Query $query WP Query object
*/
public function exclude_posts_from_search( $filters, $args, $query ) {
$bypass_exclusion_from_search = is_admin() || ! $query->is_search();
$bypass_exclusion_from_search = ( is_admin() && ! wp_doing_ajax() ) || ! $query->is_search();

/**
* Filter whether the exclusion from the "exclude from search" checkbox should be applied
*
Expand Down
40 changes: 40 additions & 0 deletions tests/php/indexables/TestPost.php
Original file line number Diff line number Diff line change
Expand Up @@ -8239,6 +8239,46 @@ public function testExcludeFromSearchQuery() {
$this->assertEquals( 2, $query->post_count );
}

/**
* Tests query doesn't return the post in if `ep_exclude_from_search` meta is set even in AJAX context
*
* @since 5.1.4
* @group post
*/
public function test_exclude_from_search_query_in_ajax() {
// As we explicitly check if we are in admin context, setting it up here to be sure it mirrors the expected context
set_current_screen( 'ajax' );
add_filter( 'wp_doing_ajax', '__return_true' );
$this->assertTrue( is_admin() );
$this->assertTrue( wp_doing_ajax() );

add_filter( 'ep_ajax_wp_query_integration', '__return_true' );

$this->ep_factory->post->create_many(
2,
array(
'post_content' => 'find me in search',
'meta_input' => array( 'ep_exclude_from_search' => false ),
)
);
$this->ep_factory->post->create(
array(
'post_content' => 'exclude from search',
'meta_input' => array( 'ep_exclude_from_search' => true ),
)
);

ElasticPress\Elasticsearch::factory()->refresh_indices();

$args = array(
's' => 'search',
);
$query = new \WP_Query( $args );

$this->assertTrue( $query->elasticsearch_success );
$this->assertEquals( 2, $query->post_count );
}

/**
* Tests that post meta value should be empty when it is not set.
*
Expand Down
Loading