• Resolved ordo273

    (@ordo273)


    Dear Support,
    while trying to resolve relationship fields into their post title using the filter: relevanssi_custom_field_value
    it always returns Updating failed. The response is not a valid JSON response.

    I’ve tried following your example on https://www.relevanssi.com/user-manual/filter-hooks/relevanssi_custom_field_value/ but even returning an empty array (or returning the $meta_value directly) does not work.)

    I would be happy to recieve any help or guidance. It seems to me the documentation for the filter might not have been updated?

    WP: 6.5.2
    PHP: 8.2.18 (Supports 64bit values)
    Server: Apache/2.4.59 (Unix)
    Plugin: 4.22.2

    With kind Regards
    Ordo

    Ps. the Code:

    add_filter( 'relevanssi_custom_field_value', 'rlv_connect_relationships_in_fields' );
    
    function rlv_connect_relationships_in_fields( $meta_value, $meta_key, $post_id ) {
    $new_meta_values = array();
    switch ($meta_key) {
    case 'buecher':
    case 'autoren':
    foreach ( $meta_value as $related_post ) {
    
    $new_meta_values[] = ' ' . get_the_title( $related_post );
    
    }
    break;
    case 'date':
    foreach ( $meta_value as $date ) {
    
    $new_meta_values[] = substr($date, 0, 4);
    
    }
    break;
    default:
    $new_meta_values[] = $meta_value;
    
    }
    return $new_meta_values;
    
    }
Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Author Mikko Saari

    (@msaari)

    This is problematic:

    $new_meta_values[] = $meta_value;

    It should be:

    $new_meta_values = $meta_value;

    You’re not looping the $meta_value array there, so $meta_value already is an array.

    You should also call the add_filter() with the right parameter count:

    add_filter( 'relevanssi_custom_field_value', 'rlv_connect_relationships_in_fields', 10, 2 );

    Try fixing these and see if that helps.

    Thread Starter ordo273

    (@ordo273)

    Heyya,

    thank you very much for the quick response!

    I indeed just used the example code without thinking about the example beeing a special case. The function only having one parameter (which works because it is the default value…). Yeah, should have been able to solve it myself. Thank you for clearing up my confusion!

    Best regards

    Kelvin

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘relevanssi_custom_field_value filter throws error’ is closed to new replies.