The Relationship field creates a very attractive version of the post object field. With a Relationship field, you can select from pages + posts + custom post types. This field is useful for advanced linking to another page / post object.

Creating a Relationship field

The Relationship field contains options to customize your field:

  • Post type: You can filter the choices by selecting post types.
  • Filter from Taxonomy: You can filter the choices even more by selecting specific taxonomies / categories
  • Maximum Posts: You can set the maximum number of posts allowed to be selected. Leave this field blank or set to -1 for infinite selections.

Edit screen

Template usage

The API will return an array of post objects in the same way that the get_posts function would.

<?php
 
/*
*  View array data (for debugging)
*/
 
var_dump( get_field('relationship') );
 
/*
*  Loop through post objects ( setup postdata )
*  Using this method, you can use all the normal WP functions as the $post object is temporarily initialized within the loop
*  Read more: http://codex.wordpress.org/Template_Tags/get_posts#Reset_after_Postlists_with_offset
*/
 
$posts = get_field('relationship');
 
if( $posts ): ?>
	<ul>
	<?php foreach( $posts as $post): // variable must be called $post (IMPORTANT) ?>
		<?php setup_postdata($post); ?>
	    <li>
	    	<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
	    	<span>Post Object Custom Field: <?php the_field('field_name'); ?></span>
	    </li>
	<?php endforeach; ?>
	</ul>
	<?php wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly ?>
<?php endif;
 
/*
*  Loop through post objects ( don't setup postdata )
*  Using this method, the $post object is never changed so all functions need a seccond parameter of the post ID in question.
*/
 
$posts = get_field('relationship');
 
if( $posts ): ?>
	<ul>
	<?php foreach( $posts as $post_object): ?>
	    <li>
	    	<a href="<?php echo get_permalink($post_object->ID); ?>"><?php echo get_the_title($post_object->ID); ?></a>
	    	<span>Post Object Custom Field: <?php the_field('field_name', $post_object->ID); ?></span>
	    </li>
	<?php endforeach; ?>
	</ul>
<?php endif;
 
/*
*  Query posts for a relationship value.
*  This method uses the meta_query LIKE to match the string "123" to the database value a:1:{i:0;s:3:"123";} (serialized array)
*/
 
$posts = get_posts(array(
	'post_type' => 'post',
	'meta_query' => array(
		array(
			'key' => 'custom_relationship', // name of custom field
			'value' => '"123"', // matches exaclty "123", not just 123. This prevents a match for "1234"
			'compare' => 'LIKE'
		)
	)
));
 
if( $posts )
{
	//...
}
 
?>

Customize the interface

The relationship field passes all results (posts) through an easy to use filter. This allows you to customize the interface by adding in custom fields, images and alike!

Here’s a snippet to add featured images to all results shown in the relationship field!

<?php 
 
add_filter( 'acf/fields/relationship/result', 'acf_relationship_result', 10, 2);
 
function acf_relationship_result( $html, $post )
{
	// vars
	$image = get_the_post_thumbnail( $post->ID, array(21, 21) );
 
	// new html
	$new = '<div style="width:21px; height:21px; background:#F9F9F9; border:#E1E1E1 solid 1px; float:left; margin:-3px 5px 0 0;">';
	if( $image )
	{
		$new .= $image;
	}
	$new .= '</div>';
 
    return $new . $html;
}
 
?>