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

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

Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #30407
    TaufanTaufan
    Participant

    Hi,
    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.

    #30419
    Ernest MarcinkoErnest Marcinko
    Keymaster

    Hi,

    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;
    }
    #30428
    TaufanTaufan
    Participant

    I’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.

    #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.

    #30435
    TaufanTaufan
    Participant

    Hi,
    Thank you very much,
    It works now.
    ?

    #30436
    Ernest MarcinkoErnest Marcinko
    Keymaster

    Actually, 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);
    }
    #30443
    TaufanTaufan
    Participant

    Your 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 200

    Can I make it happen?

    #30444
    Ernest MarcinkoErnest Marcinko
    Keymaster

    Actually, 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;
    	}
    }
Viewing 8 posts - 1 through 8 (of 8 total)
  • The topic ‘How to Max Length CustomField result to 100’ is closed to new replies.