I found a solution to this.
The plugin uses wp_searchable_posts index for the backend.
In includes/class-algolia-settings.php there is a function.
public function get_native_search_index_id() {
return (string) apply_filters( 'algolia_native_search_index_id', 'searchable_posts' );
}
We create another function under it.
public function get_native_search_index_id2() {
return (string) apply_filters( 'algolia_native_search_index_id', 'posts_product' );
}
In /includes/class-algolia-plugin.php, we change this:
$index_id = $this->settings->get_native_search_index_id();
$index = $this->get_index( $index_id );
if ( null === $index ) {
return;
}
new Algolia_Search( $index );
}
to this:
$index_id = $this->settings->get_native_search_index_id();
$index = $this->get_index( $index_id );
if ( null === $index ) {
return;
}
$index_id2 = $this->settings->get_native_search_index_id2();
$index2 = $this->get_index( $index_id2 );
if ( null === $index2 ) {
return;
}
new Algolia_Search($index, $index2 );
}
Now the index is all post types and index2 is only product post.
In includes/class-algolia-search.php
We change this
try {
$results = $this->index->search( $query->query['s'], $params, $order_by, $order );
} catch ( AlgoliaException $exception ) {
error_log( $exception->getMessage() ); // phpcs:ignore -- Legacy.
return;
}
to this
global $wp_query;
$post_type = get_query_var('post_type');
if( $wp_query->is_search && $post_type == 'product' )
{
try {
$results = $this->index2->search( $query->query['s'], $params, $order_by, $order );
} catch ( AlgoliaException $exception ) {
error_log( $exception->getMessage() ); // phpcs:ignore -- Legacy.
return;
}
}
else {
try {
$results = $this->index->search( $query->query['s'], $params, $order_by, $order );
} catch ( AlgoliaException $exception ) {
error_log( $exception->getMessage() ); // phpcs:ignore -- Legacy.
return;
}
}
Now, when you search normally, the results are coming from wp_searchable_posts index. But when you filter the results with post type and post type is the product, the results are coming from wp_posts_product which is only product items are indexed.
-
This reply was modified 2 years, 3 months ago by Ahmet.
-
This reply was modified 2 years, 3 months ago by Ahmet.
-
This reply was modified 2 years, 3 months ago by Ahmet.
-
This reply was modified 2 years, 3 months ago by Ahmet.
-
This reply was modified 2 years, 3 months ago by Ahmet.
-
This reply was modified 2 years, 3 months ago by Ahmet.