acf/fields/relationship/result

Overview

This hook allows you to modify the result (text) displayed for each post in the relationship field list.

Changelog

  • 5.0.0 – Changed 4th parameter from $post to $post_id
  • 4.1.2 – Added filter

Parameters

filter( $title, $post, $field, $post_id )
  • $title (string) the text displayed for this post object
  • $post (object) the post object
  • $field (array) the field array containing all attributes & settings
  • $post_id (int) the current post ID being edited

Usage

There are 3 ways to hook into this filter

  1. acf/fields/relationship/result – filter for any field
  2. acf/fields/relationship/result/name={$field_name} – filter for a specific field based on it’s name
  3. acf/fields/relationship/result/key={$field_key} – filter for a specific field based on it’s key

functions.php

<?php

function my_relationship_result( $title, $post, $field, $post_id ) {
	
	// load a custom field from this $object and show it in the $result
	$page_views = get_field('page_views', $post->ID);
	
	
	// append to title
	$title .= ' [' . $page_views .  ']';
	
	
	// return
	return $title;
	
}



// filter for every field
add_filter('acf/fields/relationship/result', 'my_relationship_result', 10, 4);


// filter for a specific field based on it's name
//add_filter('acf/fields/relationship/result/name=my_relationship', 'my_relationship_result', 10, 4);


// filter for a specific field based on it's key
//add_filter('acf/fields/relationship/result/key=field_508a263b40457', 'my_relationship_result', 10, 4);

?>

Related

Hi all. Server maintenance scheduled this Sunday 28th May. Learn more