Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Author WPSolr free

    (@wpsolr)

    Yes, you can.

    All filters/actions are defined in file classes/wpsolr-filters.php:

    // Customize a post custom fields before they are processed in a Solarium update document
    const WPSOLR_FILTER_POST_CUSTOM_FIELDS = 'wpsolr_filter_post_custom_fields';

    It’s used in classes/solr/wpsolr-index-solr-client.php:

    function set_custom_fields( $solarium_document_for_update, $post ) {
    
    		$custom                    = $this->solr_indexing_options['cust_fields'];
    		$custom_fields_values_list = array();
    		$aCustom                   = explode( ',', $custom );
    		if ( count( $aCustom ) > 0 ) {
    			if ( count( $custom_fields = get_post_custom( $post->ID ) ) ) {
    
    				// Apply filters on custom fields
    				$custom_fields = apply_filters( WpSolrFilters::WPSOLR_FILTER_POST_CUSTOM_FIELDS, $custom_fields, $post->ID );
    Thread Starter Fred Tibo

    (@fred-tibo)

    The apply_filters function only support 2 parameters, function apply_filters( $tag, $value ) { that’s probably why I receive the $custom_fields but the postId is always not set.

    That’s the change I propose:

    instead of: $custom_fields = apply_filters( WpSolrFilters::WPSOLR_FILTER_POST_CUSTOM_FIELDS, $custom_fields, $post->ID );

    use

    $args = ['custom-fields' => $custom_fields,'post-id' => $post->ID];
    apply_filters_ref_array(WpSolrFilters::WPSOLR_FILTER_POST_CUSTOM_FIELDS, array( &$args ));

    and to use the filter you can do something like

    add_filter(WpSolrFilters::WPSOLR_FILTER_POST_CUSTOM_FIELDS, function (&$args)
    			{
    				$custom_fields = $args['custom-fields'];
    				$custom_fields['test_1'] = 'test 1 value';
    				$custom_fields['test_2'] = 'test 2 value';
    				$custom_fields['test_post_id'] = $args['post-id'];});
    Plugin Author WPSolr free

    (@wpsolr)

    To receive the 2 parameters (2 is nb of args):

    add_filter( WpSolrFilters::WPSOLR_FILTER_POST_CUSTOM_FIELDS, array(
    			$this,
    			'my_callback_function'
    		), 10, 2 );

    You can find an example in file classes/extensions/s2member/plugin-s2member.php

    Thread Starter Fred Tibo

    (@fred-tibo)

    Thanks for your answer, I didn’t know that. A little last oen for today.

    My custom fields are not send to solr, only custom fields configure in wpsolr indexing options. Because I have json coming from an external system and I only need this data in solr for the search, I don’t have custom fields.

    add_filter(WpSolrFilters::WPSOLR_FILTER_POST_CUSTOM_FIELDS, function ($custom_fields, $postId)
    {
    	$json = get_post_meta($postId,'json');
    	$obj = json_decode($json, false);
    
    	/* not existing fields in wpsolr indexing options */
    	$custom_fields['test_1_str'] = $obj->test1;
    	$custom_fields['test_2_str'] = $obj->test2;
    	$custom_fields['test_post_id_dt'] = $obj->date;
    
    	return $custom_fields;
    }, 10, 2);
    Plugin Author WPSolr free

    (@wpsolr)

    Yes, WPSOLR only indexes “real” custom fields. This filter is only used to customize those, not to create new ones.

    So, from your example, you should:

    - create custom fields "test_1" and "test_2"
    - configure those custom fields to be indexed and (optionally) faceted

    _str is added automatically to field names, to benefit from the dynamic type *_str defined in schema.xml.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Solr filter’ is closed to new replies.