• Resolved Jon Dingman

    (@jdingman)


    Great plugin, thank you so much.

    I’m running into a case where I want to be able to select specific CPTs and not all posts.

    There is a toggle to select/deselect “All Posts” which is what I want to use, however, selecting individual CPTs creates unique indexes, which then I have to create a way to search through multiple indexes, which is not straight forward.

    Is there a way to select which CPTs I want to be indexed, and have them create a single index with just those selected CPTs?

Viewing 7 replies - 1 through 7 (of 7 total)
  • Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    Perhaps this from class-algolia-plugin.php ?

    $searchable_post_types = (array) apply_filters( 'algolia_searchable_post_types', $searchable_post_types );
    $this->indices[]       = new Algolia_Searchable_Posts_Index( $searchable_post_types );
    

    Otherwise I’m not personally sure at the moment and would need to tap shoulders of others involved with the plugin.

    Thread Starter Jon Dingman

    (@jdingman)

    @tw2113 Ah good find. Are you thinking that I Would apply the filter of algolia_searchable_post_types and simply pass in the post types I want to search on?

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    that’s what I’d end up trying out along with trying to mentally trace through the source code where needed to bolster assumptions.

    Thread Starter Jon Dingman

    (@jdingman)

    @tw2113 Thanks – I tried that with no success.

    I found an implementation that someone else wrote too, which looked close

    https://gist.github.com/rayrutjes/6b1de96b3687b937086428c37bf144f9

    Just not sure how to implement this into a single index or have it query the two indices.

    Simply don’t want to use the “all posts” because it defeats the purpose of selecting my own.

    I’m using a frontend implementation of instantsearch to connect to my index as well.

    Thread Starter Jon Dingman

    (@jdingman)

    @tw2113 any further thoughts? TIA!

    Plugin Contributor Richard Aber

    (@richaber)

    Howdy @jdingman

    The checkboxes on the WP-Admin -> Algolia Search -> Autocomplete screen are to configure Autocomplete, not to configure the search page or the searchable_posts index itself.

    The “All Posts” (searchable_posts) index is used by the Instantsearch.js (“Use Algolia with Instantsearch.js”) and PHP backed search (“Use Algolia in the backend”) integrations configured at WP-Admin -> Algolia Search -> Search Page. If you are trying to affect the search page specifically, then searchable_posts is indeed the index you want to work with.

    By default, the searchable_posts index will include content from all post types that were registered with exclude_from_search set to false. That is to say, post types that are normally searchable anyway (refer to Algolia_Plugin::load_indices()).

    After retrieving the array of registered searchable post types, they are passed through the algolia_searchable_post_types filter to allow for developer customization.

    To limit the post types available to the searchable_posts index, you would want to hook into algolia_searchable_post_types, and return an array that _only contains_ the post types you _do_ want to index.

    For example, if I wanted to limit the post types that are allowed in the searchable_posts index to just two specific post types “book” and “movie”, I could do something like this:

    
    /**
     * Override the post types used for the "All Posts" (<code>searchable_posts</code>) index.
     *
     * @param array $searchable_post_types Array of searchable post types.
     *
     * @return array
     */
    function myprefix_override_searchable_post_types( $searchable_post_types ) {
    	return [
    		'book',
    		'movie',
    	];
    }
    
    add_filter( 'algolia_searchable_post_types', 'myprefix_override_searchable_post_types' );
    

    After applying that filter in code, you would need to re-index by clicking the “Re-index search page records” button at top of WP-Admin -> Algolia Search -> Search Page, or by using the wp algolia reindex WP-CLI command.

    For clarification, as I think there may be some confusion around the checkboxes, the checkboxes on WP-Admin -> Algolia Search -> Autocomplete screen are for configuring one or more indexes to be used by the Autocomplete.js integration, which is generally tied to a search field in a widget, header, footer, not tied to the “search page” specifically. The “All Posts” (searchable_posts) index is available as a checkbox there, so that Autocomplete can use the same index that the search page uses. Alternatively, specific post types can be selected so that Autocomplete creates a new index for each individual post type. Searches in the Autocomplete drop down will then display headers for each index that was enabled on the WP-Admin -> Algolia Search -> Autocomplete screen.

    Does that information help?

    Thread Starter Jon Dingman

    (@jdingman)

    This worked perfectly. Thanks @richaber!

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Being able to select specific Custom Post Types’ is closed to new replies.