Skip to content

Im Trying To

Michael Beckwith edited this page Mar 21, 2023 · 13 revisions

Customize Autocomplete

See Customizing Templates and Customizing the Autocomplete template file

Customize InstantSearch

See Customizing Templates and [Customizing the Instantsearch template file

Customize content being indexed

Control whether content should be indexed on a per-item basis

If you need to control whether or not content is indexed, on a per-item basis, you can use the following filters. Each filter includes a boolean value for whether or not to index, as well as the current item object being considered. If a post, it'll be a WP_Post object, terms will receive a WP_Term object, and users will receive a WP_User object. You will want to return true if the item should still be indexed, or false if it should not be indexed.

Posts

Searchable/Instantsearch: algolia_should_index_searchable_post

Autocomplete: algolia_should_index_post

function wds_algolia_filter_post( $should_index, WP_Post $post )
{
    if ( 1 === $post->ID ) {
        return false;
    }

    return $should_index;
}
add_filter( 'algolia_should_index_searchable_post', 'wds_algolia_filter_post', 10, 2 );
add_filter( 'algolia_should_index_post', 'wds_algolia_filter_post', 10, 2 );

Terms

algolia_should_index_term

function wds_algolia_filter_term( $should_index, WP_Term $term )
{
    if ( 1 === $term->term_id ) {
        return false;
    }

    return $should_index;
}
add_filter( 'algolia_should_index_term', 'wds_algolia_filter_term', 10, 2 );

Users

algolia_should_index_user

function wds_algolia_filter_user( $should_index, WP_Term $user )
{
    if ( 1 === $user->ID ) {
        return false;
    }

    return $should_index;
}
add_filter( 'algolia_should_index_user', 'wds_algolia_filter_user', 10, 2 );

Add custom meta data to my indexed post objects.

Even though Advanced Custom Fields is specific to Advanced Custom Fields, the code is largely the same even if you're not using ACF. You would need to change the examples from using get_field() to get_post_meta().

Add extra image sizes to indexed post objects

By default WP Search with Algolia only indexes the thumbnail image size, but if you want to have more added, you can make use of the following filter.

<?php
function wds_algolia_index_more_image_sizes( array $sizes ) {
    $sizes[] = 'medium_large';
    $sizes[] = 'large';

    return $sizes;
}
add_filter( 'algolia_post_images_sizes', 'wds_algolia_index_more_image_sizes' );

Restricting specific content types from being indexed

Autocomplete

Make use of algolia_excluded_post_types and algolia_excluded_taxonomies filters to exclude specific content types. This will remove them from the Autocomplete settings page.

<?php
function wds_algolia_exclude_custom_post_type( array $exclude ) {
    $exclude[] = 'movie';

    return $exclude;
}
add_filter( 'algolia_excluded_post_types', 'wds_algolia_exclude_custom_post_type' );
<?php
function wds_algolia_exclude_custom_taxonomies( array $exclude ) {
    $exclude[] = 'genre';

    return $exclude;
}
add_filter( 'algolia_excluded_taxonomies', 'wds_algolia_exclude_custom_taxonomies' );

Instantsearch

You will need to intercept the found searchable post types and remove them from an array, instead of pushing the post type into an array. This is reverse from the available filter for Autocomplete.

<?php
function wds_algolia_exclude_searchable_post_types( $searchable_post_types ) {
	$remove = [ 'movie', 'tv_show' ];
	foreach( $searchable_post_types as $key => $type ) {
		if ( in_array( $key, $remove ) ) {
			unset( $searchable_post_types[ $key ] );
		}
	}
	return $searchable_post_types;
}
add_filter( 'algolia_searchable_post_types', 'wds_algolia_exclude_searchable_post_types' );
Clone this wiki locally