Reputation: 1565
I'm using Pods to set up Custom Post Types in WordPress.
I have two custom post types: Car and Garage. In Car I have set up a relationship field with Garage, so when editing a Car post I can select one of the Garage posts available from the drop-down.
When viewing a Car post, I would like to display the name and the link to the garage post selected. How would I do that?
Below is an example from http://pods.io/tutorials/get-values-from-a-custom-relationship-field
//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="'.get_permalink($id).'">'.get_the_title( $id ).'</a>';
//get the value for some_field in related post and echo it
} //end of foreach
} //endif ! empty ( $related )
Instead of a permalink to a Garage post, I'm getting several permalinks to the Car post I'm viewing.
Any Ideas?
Upvotes: 0
Views: 2500