This website uses cookies to personalize your experience. By using this website you agree to our cookie policy.

Reply To: How to Max Length CustomField result to 100

Home Forums Product Support Forums Ajax Search Pro for WordPress Support How to Max Length CustomField result to 100 Reply To: How to Max Length CustomField result to 100

#30433
Ernest MarcinkoErnest Marcinko
Keymaster

Sorry, I was not able to test before posting. Now I did, and there was indeed an error in the code. This one will work:

add_filter( 'asp_results', 'asp_c_field_to_desc', 10, 1 );
function asp_c_field_to_desc( $results ) {
    $custom_field = "my_custom_field";
    $length = 100;

    // ------ DO NOT CHANGE BELOW ----------
    foreach ($results as $k=>&$r) {
        if ($r->content_type != "pagepost") continue;
        if ( function_exists('get_field') )
            $meta_value = get_field( $custom_field, $r->id, true ); // ACF support
        else
            $meta_value = get_post_meta( $r->id, $custom_field, true );
        // Modify the post title to add the meta value
        if ( !empty($meta_value) ) {
            $meta_value = wd_array_to_string($meta_value);
            $r->content .= wd_substr_at_word($meta_value, $length);
        }
    }

    return $results;
}

Don’t add the {my_custom_field} variable to the advanced description field, the code automatically appends it to the end of it.