Strattic Developer Docs

Customizing Algolia Search Results

There’s a lot you can do to customize your Algolia search results… Check it out!

Flushing the Algolia Index

If you ever need to re-index your Strattic Algolia index (and you probably will if you’re making changes with the following filters), you can always “force sync” to Algolia by going to yoursite.site.strattic.io/wp-admin/?strattic_search_index_update=force on your website. This will cause the Algolia index to re-sync with your current setup, including any of the below filters or customizations.

If you’re looking to customize the search page or templates, see our documentation on Customizing Search Results, Templates, and Tags.

Limit search results for specific post types

The following snippet will limit search results to the “posts” post type.

We currently don’t have the option to search within a specific custom post type.

If that’s something you’d like us to add, contact us.

Note that strattic_search_item is called on each item going into Algolia.

<?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 data

Note that strattic_search_data is called on the whole array of items going into Algolia.

<?php

add_filter( 'strattic_search_data', 'search_data_filter' );
function search_data_filter( $data ) {
    foreach ( $data as $key => $value ) {
        if ( false === $value ) {
            unset ( $data[ $key ] );
        }
    }
    return $data;
}

Filter search formatting

<?php

add_filter( 'strattic_search_formatting', 'add_type', 10, 2 );
function add_type( $search_data, $page_data ) {
    $search_data['terms_item_type']['name'] = "sample" . $page_data['id'];
    return $search_data;
}

Add custom key to Algolia index

This code snippet can be used to add additional or custom fields to the Algolia search index:

<?php
/**
* Allow changes to data discovered for an item in search results. This is called before the function that formats the data for sending to Algolia.
*/

add_filter(
	'strattic_search_item',
	function( $item ) {

		$post_id = $item['id'];
		$item['thumbnails'] = array();
		$item['thumbnails']['full'] = str_replace( home_url(), '', get_the_post_thumbnail_url( $post_id, 'full' ) );

		return $item;
	}
);

/**
* Allow changes to data that will be sent to Algolia for an item in search results. This is called after the function that formats the data for sending to Algolia.
*/

add_filter(
	'strattic_search_formatting',
	function( $item, $page ) {
		$item['thumbnails'] = $page['thumbnails'];

		return $item;
	},
	null,
	2
);

Adding the post type attribute

<?php

/**
* Allow changes to data discovered for an item in search results. This is called before the function that formats the data for sending to Algolia.
*/
add_filter(
	'strattic_search_item',
	function( $item ) {

		$post_id = $item['id'];
		$item['post_type'] = get_post_type( $post_id );

		return $item;
	}
);

/**
* Allow changes to data that will be sent to Algolia for an item in search results. This is called after the function that formats the data for sending to Algolia.
*/
add_filter(
	'strattic_search_formatting',
	function( $item, $page ) {
		$item['post_type'] = $page['post_type'];

		return $item;
	},
	null,
	2
);