Showing Post Object field values in the ACF back-end
written by: Jeff McNearShowing Post Object field values in the ACF back-end
WordPress is not really designed to allow a great degree of flexibility to how things are presented on the back-end… and it is becoming even less so as Gutenberg progresses.
A recent request was to show field values from a related post connected to a post by a “post object” field.
Based on this article: https://support.advancedcustomfields.com/forums/topic/show-post-objects-fields-in-backend/ I was able to put this block of code together which will show the specified field values on the back-end after the initial save of the post. (code to be placed in the functions file)
function action_function_name( $field ) {
if( $field['value'] ){
$thepost = get_post($field['value']);
$permalink = get_permalink( $thepost->ID );
echo '<ul style="margin-left:2em">' ;
// echo '<li >'. '<a href=" ' .get_permalink( $thepost->ID ). '" target="_blank">' .
//$thepost->post_title . '</a>'
//.'</li>';
echo '<li >'.'<strong>'.'Primary Contact'.'</strong>' . ' ' . $thepost->client_primary_contact_name . '<br/>' . '<strong>'.'Title:' . '</strong>' . ' ' .$thepost->client_primary_contact_title . '<br/>'.'<strong>'.'Email:'.'</strong>' . ' ' . $thepost->client_primary_contact_email.'<br/>'.'<strong>'.'Phone:'.'</strong>' . ' ' . $thepost->client_primary_contact_phone.
'</li>' ;
echo '<li >'.'<strong>'.'Secondary Contact'.'</strong>' . ' ' . $thepost->client_secondary_contact_name . '<br/>' . '<strong>'.'Title:' . '</strong>' . ' ' .$thepost->client_secondary_contact_title . '<br/>'.'<strong>'.'Email:'.'</strong>' . ' ' . $thepost->client_secondary_contact_email.'<br/>'.'<strong>'.'Phone:'.'</strong>' . ' ' . $thepost->client_secondary_contact_phone.
'</li>' ;
echo '<li >'.'<strong>'.'Main Billing Contact'.'</strong>' . ' ' . $thepost->client_billingy_contact_name . '<br/>' . '<strong>'.'Title:' . '</strong>' . ' ' .$thepost->client_billing_contact_title . '<br/>'.'<strong>'.'Email:'.'</strong>' . ' ' . $thepost->client_billing_contact_email.'<br/>'.'<strong>'.'Phone:'.'</strong>' . ' ' . $thepost->client_billing_contact_phone.
'</li>' ;
echo '</ul>';
echo '<strong>'. 'For additional contacts please see the Client post' . ' ' . '<a href=" ' .get_permalink( $thepost->ID ). '" target="_blank">' .'here' . '</a>'.
'</strong>';
}
}
add_action( 'acf/render_field/type=post_object', 'action_function_name', 20, 1 );
It is important to note that I was unable to get any kind of conditional language to work …. capabilities are much more limited here than would be when putting to gether a front end template.