Below is a function which will do what you have described. It can be installed as a plugin by placing it in wp-content/plugins
, or can be added to a theme’s functions.php. A Code Snippets plugin can also be used, but that is generally not recommended for many reasons.
<?php
/**
* Plugin Name: User Relationship Display
* Description: Filter Pods user relationship field by the name <code>user_relationship</code> to display the user display name and field <code>type_field</code>.
* Version: 1
*/
add_filter(
'pods_field_pick_data',
function ( $data, $field_name, $value, $options, $pod, $id ){
$field_to_filter = 'user_relationship'; // This is the name of the field which is a user relationship field.
$type_user_meta_key = 'type_field'; // This is the name of the field referred to as "type".
if ( $field_to_filter === $field_name ) {
foreach ( $data as $user_id => & $value ) {
if( ! empty( $user_id ) ) {
$user = get_user_by( 'id', $user_id );
$user_pod = pods( 'user', $user_id );
$type = $user_pod->display( $type_user_meta_key );
// Default to the user display name.
$value = $user->display_name;
// Add the type field after a hyphen if it is not empty.
if ( ! empty( $type ) ) {
$value = sprintf(
'%s - %s',
$value,
$type
);
}
}
}
}
return $data;
},
20,
6
);
The code above is an adaptation of https://docs.pods.io/code-snippets/multiple-fields-pick-list/. It has these differences from the documentation:
- The name of your user relationship field is assumed to be
user_relationship
. In the linked example, this was incorrectly noted as pods_field_RELPODNAME
. There is no prefix. It is the key of the relationship field.
- The name of your
type
field is assumed to be type_field
. In this case, type_field
was used because when I attempted to create a field named type
, Pods said it was not allowed.
- The value of
type
is accessed with $user_pod->display( $type_user_meta_key )
. This will apply appropriate formatting no matter what datatype you have selected for the field. This is the same as $p->display('RELATIONSHIPFIELD.name')
in the linked example —?note how dots can be used to traverse relationships, as $p
and $user_pod
are an instantiated Pods object.
$user_pod = pods( 'user', $user_id );
above is the same as $p = pods('RELPODNAME', $dataid);
in the example. The idea is that the first argument, user
or RELPODNAME
is the name of the Pod post type or object type, in your case user
, and $dataid
or $user_id
is the ID of the object displayed in the relationship field — it could be a post, a user, a file attachment, a taxonomy term, etc.: it will always be the ID of the object.
Once the file is running, you should only need to change user_relationship
to the name of your relationship field and type_field
to the name of the field you referred to as type
.