Building a custom facet from an ACF post type relationship

  • pseudo visitor
    Participant
    5 years, 4 months ago #8685

    What does not work in practice is the “writers” field (or “writers_str”) that is not indexed while I’m filling it and adding it to the $custom_fields array.

    wpsolr
    Keymaster
    5 years, 4 months ago #8686

    It does not appear in the index?

    pseudo visitor
    Participant
    5 years, 4 months ago #8687

    No, and even in the Solr console directly, another custom ACF field checked in the 2.2 tab appears, but not this one.

    wpsolr
    Keymaster
    5 years, 4 months ago #8688

    Can I see the new hook code?

    wpsolr
    Keymaster
    5 years, 4 months ago #8689

    The new field must be one of the fields selected in 2.2.

    Make it appear in 2.2 with the code below, then select it with its type:

    
    add_filter( WPSOLR_Events::WPSOLR_FILTER_INDEX_CUSTOM_FIELDS, [
     $this,
     'add_my_field_to_indexed_custom_fields',
    ], 10, 2 );
    
    
    /**
     * Add a field to indexed fields in 2.2
     *
     * @param string[] $custom_fields
     * @param string $model_type
     *
     * @return array
    */
    public function add_my_field_to_indexed_custom_fields( $custom_fields, $model_type ) {
    
    if ( ! isset( $custom_fields ) ) {
    $custom_fields = [];
    }
    
    switch ( $model_type ) {
    case 'post':
    case 'page': // add other post types if you like
    if ( ! in_array( 'my-field-name', $custom_fields, true ) ) {
    array_push( $custom_fields, 'my-field-name' );
    }
    break;
    }
    
    return $custom_fields;
    }
    
    pseudo visitor
    Participant
    5 years, 4 months ago #8692

    Okay, it’s finally good!

    I was confused in the blur of the difference between indexing and customizing the indexed data. It had to be done first a WPSOLR_FILTER_INDEX_CUSTOM_FIELDS to add a field with a name that is not “writers” which is a relation field (because with the relation field checked it crashes at indexing, expect string object given), then fill in the new field with the desired data with WPSOLR_FILTER_POST_CUSTOM_FIELDS.

    Thank you for being so patient! I send you a Gist once the code is cleaned, it could serve as an example to others 🙂

Viewing 6 posts - 16 through 21 (of 21 total)

You must be logged in to reply to this topic.