Get Values From A Relationship Field

relationship-multi-checkboxesIn this quick tutorial we will show you how to get values from a Pods’ relationship field, or a bi-directional relationship field between two different post types. There are many possible types of relationship fields, but one of the most common and powerful uses for them is to relate one type of post type to another. With these types of relationships posts in one post type (custom or not) become values for a field in another post type.

UPDATE: March 19, 2014- The code in this article assumes the related post is a custom post type, not an Advanced Content Type. We have added a new section and example code for use when the related post is an Advanced Content Type at the end of this article.

We are not going to cover how to create relationship fields or bi-directional fields as this is short tutorial and there are detailed instructions on creating relationship fields, including bi-directional relationships on the page for relationship fields. For this tutorial we will be showing how to get the titles of related posts and show them as links along with the value of a related field.

You can see the complete code at the bottom, but we will go through it step by step for you first.

$pod = pods( 'pod_name', get_the_id() );

The first thing you have to do is get your Pods object for the current post using pods( 'pod_name', get_the_id() ) and store it in the variable $pod. In this example, we are using get_the_id() to use the ID of the current post in the loop. Depending on your needs you may need to set the ID differently. If you are in the loop, you can use global $post; $post->ID.

Be sure to specify which Pod your relationship field is from. You might also choose to limit which posts are returned by adding additional parameters to pods(). See the docs for the Pods class for more information.

$related = $pod->field( 'relationship_field' );

From there we use Pods::field to get the value for our relationship field and store it in the variable $related with this line: $related = $pod->field( 'relationship_field' );. Now would be a good time for you to var_dump( $related ) and take a look at all of the values of that array. One thing you will see is that it is a multi-dimensional array of all of the items in the related field. The important key in each of these arrays we will need for this tutorial is post id. Once we have the post’s ID we can feed that to simple WordPress functions like get_permalink() and get_the_title() and even use get_post_meta() to get fields from the related posts.

if ( ! empty( $related ) ) {
foreach ( $related as $rel ) {

Because $related is a multi-dimensional array we will have to loop through each of the individual arrays to get the individual values. Before we start our foreach loop, it is important to make sure we have valid input for the loop. In order to avoid returning an error when there are no related posts we will wrap our foreach loop in a check to make sure $related is not empty.

$id = $rel[ 'ID' ]

Once we’ve begun our foreach loop the first and most important thing we will do is get the ID for the related post by putting the index ID in a variable $id with. Remember when we checked out the var_dump of the $related array? You can get the values of any of the keys we saw there that might be useful to you with $rel[ 'key' ].

echo '<a href="'.get_permalink( $id ).'">'.get_the_title( $id ).'';

Once we have the post id, we can do pretty much whatever we want with it. There is no need to stick to Pods methods at this point. In fact it is easier to use regular WordPress functions that you know and love. In this example I am using get_permalink( $id ) and get_the_title( $id ) to make my list of posts.

$someField = get_post_meta( $id, 'some_field', true );
echo $someField;

Because we now have the ID for the related post we can use get_post_meta(); to get the value from any of its custom fields. Note that the last parameter is set to true so that we only get one value. By default it would return an array of values, which we could send through its own foreach loop. If I hadn’t specified a specific field in the second parameter I would have gotten a multi-dimensional array of all of the meta values from the post that I could have looped through as well. I’m sure how you can see how this can get more complicated and more powerful quite quickly.

That’s about it. Just be sure to end the foreach and the check to see if $related was empty. One thing to consider is putting this whole code snippet inside a function so that you can reuse it through out your theme or plugin. If you do, be sure to add a a parameter for Pod ID or slug. Here is the complete code:

<?php 
    	//get Pods object for current post
    	$pod = pods( 'pod_name', get_the_id() );
	//get the value for the relationship field
	$related = $pod->field( 'relationship_field' );
	//loop through related field, creating links to their own pages
	//only if there is anything to loop through
	if ( ! empty( $related ) ) {
		foreach ( $related as $rel ) { 
			//get id for related post and put in ID
			//for advanced content types use $id = $rel[ 'id' ]
			$id = $rel[ 'ID' ];
			//show the related post name as link
			echo '<a href="'.esc_url( get_permalink( $id ) ).'">'.get_the_title( $id ).'</a>';
			//get the value for some_field in related post and echo it
			$someField = get_post_meta( $id, 'some_field', true );
			echo $someField;
		} //end of foreach
	} //endif ! empty ( $related )
?>

Relationship Fields In Pods Templates

To do something similar in Pods Templates, check out the documentation on Using Template Tags in Pods Templates.

Related Posts That Are Advanced Content Types

If the related post is an Advanced Content Type (ACT) the example code we showed above will need some adjustments. Because ACTs are not WordPress post types, you can’t make use of the functions from WordPress core with them. For example, get_permalink() can’t be used to get a link to an ACT, as the ACT’s ID is not the ID of a WordPress post. Instead the permalink will have to be built manually.

In the example code below be sure to read through the comments carefully, and also pay attention to any part of the code itself in all caps. The code in all caps, must be changed to match your Pods configuration. Also, note that the way the permalink is constructed matches the way I recommend in my tutorials on using ACTs. Be sure that it outputs a link that matches your actual permalink structure.

<?php
//build pods object
//NOTE: If in a Pods Template, skip this, $obj is already populated with the current Pods object.
$obj = pods( 'POD_NAME', pods_v_sanitized( 'last', 'url' ) );
//get the value for the relationship field
$related = $obj->field( 'RELATED_FIELD' );
//loop through related field, creating links to their own pages
//only if there is anything to loop through
if ( ! empty( $related ) && is_array($related) ) {
  foreach ( $related as $rel ) {
    //get id for related post and put in id
    $id = $rel[ 'id' ];
    //get value of the field name
    $name = $rel[ 'name' ];
    //get related post permalink
    $permalink = $rel[ 'permalink' ];
    //echo the related post name as link
    echo '<a href="'.esc_url( site_url( trailingslashit( 'RELATED_POD_SLUG' ) . $rel[ 'permalink' ] ) ).'">' . $name . '</a>';    
  } //end of foreach
} //endif ! empty ( $related )
?>

This code follows the same basic pattern as I explained before. Note that I used the variable $obj for the Pods object. This is so that if you are using this code in a Pods Template, you can just skip the line that builds the Pods object as in a Pods Template, the Pods object is already available in $obj.