There are two different methods for stripping post-types from search results. Specific post-types can be removed from your entire search index, or you can filter the results based on the post-type.
Removing a post-type from the Algolia search index.
The strattic_search_item
filter can be used to remove items from the search results. The following snippet will limit search results to the “post” post type. Note that strattic_search_item
is called on each item going into Algolia.
Note: You must reindex Algolia by visiting /?strattic_search_index_update=force
to ensure this change appears in your search results.
<?php
/**
* Enable only one specific post-type in Strattic search.
*/
add_filter(
'strattic_search_item',
function( $data, $post ) {
if ( 'post' !== $post->post_type ) {
$data = null;
}
return $data;
},
1,
2
);
Filter search results based on post-type
For some situations, you may not want to totally remove a post-type from your entire search index. In this situation, you need to specifically filter out those results. To do this, you need to send the post-type for each post to Algolia (set via the strattic_search_formatting
filter), set an Algolia facet (used for only returning results based on a specification you set) and to change the request sent to Algolia (achieved via editing the JavaScript variable strattic_algolia.filters
).
Here is some example code which allows you to only return results for the “post” post-type. You can adjust the JavaScript variable strattic_algolia.filters
via any method you wish. A popular option for this is via filter tabs on your search page (which you would need to implement manually).
Note: You must reindex Algolia by visiting /?strattic_search_index_update=force
to ensure this change appears in your search results.
<?php
/**
* Adds post-type specific search for Algolia.
*/
class Strattic_Search_Post_Types {
/**
* Constructor.
*/
public function __construct() {
add_action( 'wp_head', array( $this, 'inline_script' ) );
add_action( 'strattic_search_formatting', array( $this, 'send_post_type_to_algolia' ) );
add_action( 'strattic_facets', array( $this, 'algolia_facets' ) );
}
/**
* Inline script to set the Algolia filter.
*
* Can also set this in an external script or in the footer.
*/
public function inline_script() {
echo "
<script>
let strattic_algolia = [];
strattic_algolia.filters = \"'post_type': 'post'\";
</script>
";
}
/*
* Add post-type for each post sent to Algolia.
*
* @param array $item The search item.
* @return array The modified search item.
*/
public function send_post_type_to_algolia( $item ) {
$post_id = $item['id'];
$item['post_type'] = get_post_type( $post_id );
return $item;
}
/**
* Add post-type attribute for faceting in Algolia.
*
* @param array $attributes_for_faceting Attributes for faceting.
* @return array
*/
public function algolia_facets( $attributes_for_faceting ) {
$attributes_for_faceting[] = 'searchable(post_type)';
return $attributes_for_faceting;
}
}
new Strattic_Search_Post_Types;