Modifying results content before it is displayed in autocomplete suggestions

  • markus
    Participant
    3 years, 1 month ago #24994

    Hi,

    I’m trying to find a way to modify post’s content before it is displayed in search suggestions by the suggestions twig template. I would need to remove some unwanted style strings from post’s content with regex. Those strings are made by Gutenberg blocks and they are stored in the database as is.
    I already managed to do the same to results on the actual search page by using WordPress’s get_the_excerpt filter hook and checking if is_search(). Actually, I realized it could also have done with WPSOLR’s WPSOLR_ACTION_POSTS_RESULTS hook.

    So, is there some similar hook for autocomplete suggestions to modify post’s content before displaying it?

    Thank you!

    wpsolr
    Keymaster
    3 years, 1 month ago #24998

    You could instead apply your regex at indexing time. This would fix your display results for search and suggestions, but also improve your search relevance.

    You can do it with event WPSOLR_FILTER_SOLARIUM_DOCUMENT_FOR_UPDATE as seen in /wp-content/plugins/wpsolr-pro/wpsolr/pro/extensions/woocommerce/class-wpsolr-plugin-woocommerce.php:

    add_filter( WPSOLR_Events::WPSOLR_FILTER_SOLARIUM_DOCUMENT_FOR_UPDATE, [$this, 'add_fields_to_document_for_update',], 10, 5 );

    /**
     * Clean a document just before sending it to the index
     * @param array $document_for_update
     * @param $solr_indexing_options
     * @param $post
     * @param $attachment_body
     * @param WPSOLR_AbstractIndexClient $search_engine_client
     *
     * @return array Document updated with fields
     */
    function add_fields_to_document_for_update( array $document_for_update, $solr_indexing_options, $post, $attachment_body, WPSOLR_AbstractIndexClient $search_engine_client ) {
      // Clean $document_for_update
      (....)
      return $document_for_update;
    }
    markus
    Participant
    3 years, 1 month ago #25059

    That was exactly what I was looking for. Thank you!

    wpsolr
    Keymaster
    3 years, 1 month ago #25060

    If it works well, you can let me know your code here and I’ll integrate it to the WPSOLR core as a new option “Strip Gutenberg blocks style” on WPSOLR tab 2.2

    markus
    Participant
    3 years, 1 month ago #25061

    I’m going to do a few more tests to see if I can get regex to work in most cases. I’ll get back to this.

Viewing 5 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic.