WebDevStudios / wp-search-with-algolia

Improve search on your site. Autocomplete is included, along with full control over look, feel and relevance.
https://wordpress.org/plugins/wp-search-with-algolia/
141 stars 54 forks source link

Allow Attachment/ Files to be indexed in in `searchable_posts` #199

Closed samfrank closed 2 years ago

samfrank commented 2 years ago

I have one index, that contains all of my post types that I would like to be indexed and then using the Facet to filtering depeding on the post type. Keeping everything is one index make everything a lot easier for me when implementing the search on the front-end.

I am utilising the algolia_searchable_post_shared_attributes to then assign a different function to the post type which easily allows me to assign custom attributes to the $attrs. Both functions below.

    public function postSharedAttributes($attrs, $post) {
        /////////////////////////
        // Post-specific logic using dynamic functions like handleAttributes{PostType}
        /////////////////////////
        // Convert the post type into camel case
        $postType = ucfirst($this->toCamelCase($post->post_type));
        $functionName = "handleAttributes{$postType}";

        if (method_exists($this, $functionName)) {
            return $this->$functionName($attrs, $post);
        }

        return $attrs;
    }
    protected function handleAttributesAttachment($attrs, \WP_Post $post) {
        $attrs['custom_attr'] = 'PDF';

        return $attrs;
    }

I have a request, where the clients would like to index all the PDFs that have been uploaded to the site. However, it seems that the algolia_searchable_post_shared_attributes does not loop over the attachments post type?

I have printed the output of the filter of algolia_searchable_post_types and I can clearly see the the attachment is in the outputted array?

I have looked into the where the apply_filters( 'algolia_post_' . $post->post_type . '_shared_attributes', $shared_attributes, $post ) is being called in the plugin and I can see it has an if statement which is looking to see if it is an attachment. This is at line 262 at class-algolia-posts-index.php.

        if ( 'attachment' === $post->post_type ) {
            $shared_attributes['alt'] = get_post_meta( $post->ID, '_wp_attachment_image_alt', true );

            $metadata = get_post_meta( $post->ID, '_wp_attachment_metadata', true );
            $metadata = (array) apply_filters( 'wp_get_attachment_metadata', $metadata, $post->ID ); 

            $shared_attributes['metadata'] = $metadata;
        }

So I am confused on when the algolia_post_shared_attributes or algolia_searchable_post_shared_attributes does not index the attachments as well?

Expected behavior I would like the filters of algolia_post_shared_attributes or algolia_searchable_post_shared_attributes to index attachments in the same index.

Additional context I have added the additional filter of algolia_searchable_post_attachment_shared_attributes to call the same postSharedAttributes, however the attachment does not get indexed.

samfrank commented 2 years ago

I have worked it out, but will write what I found out below if anyone else has the same issue.

The attachment is not index by the plugin because it does not satisfy the if statement in class-algolia-searchable-posts-index.php in the function below:

private function should_index_post( WP_Post $post ) {
    $should_index = 'publish' === $post->post_status && empty( $post->post_password );

    return (bool) apply_filters( 'algolia_should_index_searchable_post', $should_index, $post );
}

Easy fix was to add the filter algolia_searchable_post_types in your functions file then redeclare if it should be indexed or not.

function shouldIndexPostTypes($should_index, \WP_Post $post) {
    $should_index = 'publish' === $post->post_status && empty( $post->post_password ) || 'attachment' === $post->post_type;

    return $should_index;
}