Add metafield value to ACF relationship field

The Advanced Custom Fields Relationship Field

This field adds a list of post types to connect to the current object.

In some situations it’s hard to identify posts only by their title, so it may be useful to add other useful informations. In my current project I have a meta field containing an object-number, so I want to add this before the title.

acf-relationship

ACF has a nice filter for this use case:

function add_meta_field_to_acf_relation($title, $post, $field, $post_id){
    return str_replace('</div>', '</div> <strong>'.get_field('objectnumber',$post->ID).': </strong>', $title);
}
add_filter('acf/fields/relationship/result', 'add_meta_field_to_acf_relation',10,4)

I replace the closing DIV tag from post thumbnail with my meta field.

2 Responses to “Add metafield value to ACF relationship field”

  1. Hi

    Hi can you please tell me where did you place this code.

    It would be really help full to have complete guide

    Even I am looking for the solution

    Reply

Leave a Reply