pseudo visitor
Participant
5 years, 6 months ago #6840

Here is a piece of code using WPSOLR’s filter https://www.wpsolr.com/guide/actions-and-filters/index-modify-a-document/.

Modify and copy the following  code into your theme’s functions.php, or in a new small plugin.

use wpsolr\core\classes\WPSOLR_Events;
use wpsolr\core\classes\WpSolrSchema;

add_action( 'after_setup_theme', function () {
    add_filter( WPSOLR_Events::WPSOLR_FILTER_SOLARIUM_DOCUMENT_FOR_UPDATE, 'wpsolr_filter_solarium_document_for_update', 10, 5 );
} );

/**
*
 * @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 wpsolr_filter_solarium_document_for_update( array $document_for_update, $solr_indexing_options, $post, $attachment_body, WPSOLR_AbstractIndexClient $search_engine_client ) {

 $solr_dynamic_type = WpSolrSchema::_SOLR_DYNAMIC_TYPE_STRING; // Depends on the type selected on your field on screen 2.2
 $document_for_update[ 'my_field_name' . $solr_dynamic_type] = 'whatever';
   
 return $document_for_update;
}