Strattic Developers by Elementor

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 set some results to be not indexed in Algolia, then you will need to contact Strattic support to flush your index, or the results will not be stripped from your search listings.

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

Removing results from search

The strattic_search_item filter can be used to remove items from the search results. 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
);

Add custom key to Algolia index

The strattic_search_formatting filter can be used for editing keys in the Algolia index. By default, our Algolia indexes include a reference to the thumbnail of your featured images, but not all of the other images available for that featured image. The following snippet shows how to include all of those images via the strattic_search_formatting filter.

<?php

 * Making all featured images available for Strattic search.
 * can be used like this in templates: {{thumbnails.full}}
 * Remember to flush the Algolia index to show the changes /wp-admin/?strattic_search_index_update=force
 *
 * @param array $item The search item.
 * @return array The modified search item.
 */
add_filter(
	'strattic_search_formatting',
	function( $item ) {
		$post_id            = $item['id'];
		$item['thumbnails'] = array();
		$sizes              = get_intermediate_image_sizes();
		$sizes[]            = 'full';

		foreach ( $sizes as $key => $size ) {

			$url = str_replace( home_url(), '', get_the_post_thumbnail_url( $post_id, $size ) );

			if ( '' !== $url ) {
				$item['thumbnails'][ $size ] = $url;
			}

		}

		return $item;
	}
);

The following snippet allows you to include the authors avatar via the strattic_search_formatting filter.

<?php

/**
 * Add the authors avatar to the search item.
 *
 * @param array $item The search item.
 * @return array The modified search item.
 */
add_filter(
	'strattic_search_formatting',
	function( $item ) {
		$post_id = $item['id'];

		if ( 'page' !== get_post_type( $post_id ) ) {
			return null;
		}

		return $item;
	}
);

The following snippet allows you to include a posts post-type via the strattic_search_formatting filter.

<?php

/*
 * Add post-type to search data.
 *
 * @param array $item The search item.
 * @return array The modified search item.
 */
add_filter(
	'strattic_search_formatting',
	function( $item ) {
		$post_id           = $item['id'];
		$item['post_type'] = get_post_type( $post_id );

		return $item;
	}
);