Using multiple indices on WordPress

I have a corporate site and blog which are both run on WordPress. I’m using Algolia on the corporate site, but I’d like the search results to return results from the blog that match. I’ve installed the Algolia plugin on the blog site and created an index, but Algolia on the corporate site only sees the corporate site index. Is there anyway I can get it to search against both indices? Or can I get both sites to feed into the same index? I tried this but it just overrode what was there.

1 Like

Hi @tim.ottewell,

Nice use case you have there.

Unfortunately this is not natively support by the plugin.

What you could do to make the autocomplete dropdown return results from both websites is to change the index prefix on the corporate website.

Then you can customize the autocomplete configuration by using filters to inject the indices.

Something along those lines:

<?php
add_filter('algolia_autocomplete_config', function($config) {
    $config[] = [
        'index_name' => 'corporate_posts_post',
        'enabled'    => true
    ];

    return $config;
});

@tim.ottewell thank you for your solution. It got me half way there, but I just wanted to add some additional details for anyone else who comes across this.

The solution also has an error. The key ‘enable’ needs to actually be ‘enabled’.

Also, this solution will not apply your multiple indices to the autocomplete search, only the search results page. In order for the results to show up in the autocomplete search field you also need the ‘position’, ‘max_suggestions’, and ‘tmpl_suggestion’ keys.

See my example below from my functions.php file. This fully works in autocomplete and search results page.

add_filter('algolia_autocomplete_config', function($config) {
    $config[] = [
        'index_name' => 'thm_posts_post',
        'label' => 'Posts',
        'position' => 20,
        'max_suggestions' => 5,
        'tmpl_suggestion' => 'autocomplete-post-suggestion',
        'enabled'     => true
    ];
    $config[] = [
        'index_name' => 'thm_posts_recipe',
        'label' => 'Recipes',
        'position' => 25,
        'max_suggestions' => 5,
        'tmpl_suggestion' => 'autocomplete-post-suggestion',
        'enabled'     => true
    ];
    $config[] = [
        'index_name' => 'thm_terms_post_tag',
        'label' => 'Tags',
        'position' => 30,
        'max_suggestions' => 5,
        'tmpl_suggestion' => 'autocomplete-term-suggestion',
        'enabled'     => true
    ];
    return $config;
});

Your code may need to vary to change the position of your included indices, and notice the different ‘tmpl_suggestion’ values, one is for a regular post type result and the other is for tag results.

Hope this helps anyone else who stumbles across this post looking for how to do this.

1 Like