Home › Forums › Product Support Forums › Ajax Search Pro for WordPress Support › How to Max Length CustomField result to 100
- This topic has 7 replies, 2 voices, and was last updated 5 years, 6 months ago by
Ernest Marcinko.
-
AuthorPosts
-
November 28, 2020 at 11:12 am #30407
Taufan
ParticipantHi,
My CustomField value contain 500 character.
Then in “Advanced Description Field” i add {custom_field}My question is: How to Max Length CustomField result to 100.
Thanks for your help.
I really appricated.November 30, 2020 at 10:11 am #30419Ernest Marcinko
KeymasterHi,
Well, the only way to do that is via custom coding, instead of using the advanced field, as there is no parameter to control that.
I constructed a custom code for you, which should help with that. Try adding it to functions.php in your theme/child theme directory. Before editing, please make sure to have a full site back-up just in case!
Change the $custom_field and $length variables according to your needs.
add_filter( 'asp_results', 'asp_c_field_to_desc', 10, 1 ); function asp_c_field_to_desc( $results ) { $custom_field = "field_name"; $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, $id, true ); // ACF support else $meta_value = get_post_meta( $id, $custom_field, true ); // Modify the post title to add the meta value if ( !empty($meta_value) ) { $r->content .= wd_substr_at_word($meta_value, $length); } } return $results; }November 30, 2020 at 6:32 pm #30428Taufan
ParticipantI’m sorry, but have you tried it by yourself?
because the code above that I put in functions.php has no effect.
Even after I clear cache and deactivate all plugins except AjaxSearchPro.
Can you double check your code again?I already change the code to:
$custom_field = "my_custom_field"; $length = 10;And add this to “Advanced Description Field” column:
{my_custom_field}But have no effect.
I just want to make sure the code is correct?It’s not urgent, I just learn with this plugin setting.
Thanks.
December 1, 2020 at 9:32 am #30433Ernest Marcinko
KeymasterSorry, 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.December 1, 2020 at 9:47 am #30435Taufan
ParticipantHi,
Thank you very much,
It works now.
?December 1, 2020 at 9:51 am #30436Ernest Marcinko
KeymasterActually, I may have found a much better solution. Instead of the previous code, you can use the one below. With that, you can use the advanced description field as before, it will automatically change the maximum length of all field values to 100:
add_filter('asp_cpt_advanced_field_value', 'change_asp_cpt_advanced_field_value', 10, 2); function change_asp_cpt_advanced_field_value($value, $field_name) { $length = 100; return wd_substr_at_word($value, $length); }December 1, 2020 at 10:26 am #30443Taufan
ParticipantYour last code was great.
But it cut my other custom_fields to 100 too.Say I have 3 custom_fields.
custom_field_1 = Dont set max length.
custom_field_2 = Set Max Length to 100
custom_field_3 = Set Max Length to 200Can I make it happen?
December 1, 2020 at 10:52 am #30444Ernest Marcinko
KeymasterActually, I think so, maybe this variation:
add_filter('asp_cpt_advanced_field_value', 'change_asp_cpt_advanced_field_value', 10, 2); function change_asp_cpt_advanced_field_value($value, $field_name) { // Each line 'field_name' => length $fields_length = array( 'custom_field_2' => 100, 'custom_field_3' => 200 ); if ( isset($fields_length[$field_name]) ) { return wd_substr_at_word($value, $fields_length[$field_name]); } else { return $value; } } -
AuthorPosts
- The topic ‘How to Max Length CustomField result to 100’ is closed to new replies.