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