Home › Forums › ACF PRO › How do I display a custom field in another custom post type as a shortcode?
Ok, so I’ve been trying to figure this thing out for a while now. I have two custom post types that have different sets of custom field groups. Let’s call them Post type A/B, and Custom Field Group A/B. The As go together and the Bs go together.
In Field group A, I have a group object that has a Post Object Relational Type that I’ve Linked to Post Type B. I want to be able to display Field Group B data on the front end of Post Type A from Post Type B using a shortcode.
Is there a way to accomplish this using a shortcode without extra coding like if i were to tag a group item like:
[acf field="Group_name_Relational_object_Relational_object_field"]
This is not possible using the acf shortcode. The acf shortcode only works with basic fields. It can sometimes work with other fields, for example a select field that only allows one value. But it will not work with complex fields like relationships. This would require coding your own shortcodes or possibly using some type of content editor that is capable or working with complex ACF fields.
I wouldn’t know where to start to create a shortcode, how would I link the two so that the Type B data can show up as if it were in Type A?
I would do it using PHP in the template.
The issue is not with showing the content of another post, you can do this by just supplying the post ID in the acf shortcode as long as it’s a field type that can be handled by the acf shortcode
[acf field="field_name" post_id="123"]
The issue is that you want to dynamically populate the post id using the value in the post object field.
The following is code that should get values, it is just a copy of ACF shortcode with a minor change. It assumes that the field specified is a post object field that only allows one selection.
function related_post_acf_shortcode( $atts ) {
// extract attributs
extract( shortcode_atts( array(
'field' => '',
'post_id' => false,
'format_value' => true
), $atts ) );
// get unformatted value of post object field
$related_post_id = get_field($field, $post_id, false);
// get value from the related post and return int
$value = get_field( $field, $related_post_id, $format_value);
// array
if( is_array($value) ) {
$value = @implode( ', ', $value );
}
// return
return $value;
}
add_shortcode('related_post_acf', 'related_post_acf_shortcode');
Thanks for this. Just to clarify, the ‘field’ should have the name of the Post Object field that I’m targeting the other page.
'field' => 'Post_object'
Then when calling the field on the called page I need to type it in and display it as:
[related_post_acf field=”Field_from_the_target_post”] ?
Just to clarify, the ‘field’ should have the name of the Post Object field that I’m targeting the other page.
yes
function related_post_acf_shortcode( $atts ) {
// extract attributs
extract( shortcode_atts( array(
'field' => '',
'related_field' => '',
'post_id' => false,
'format_value' => true
), $atts ) );
// get unformatted value of post object field
$related_post_id = get_field($field, $post_id, false);
// get value from the related post and return int
$value = get_field( $related_field, $related_post_id, $format_value);
// array
if( is_array($value) ) {
$value = @implode( ', ', $value );
}
// return
return $value;
}
add_shortcode('related_post_acf', 'related_post_acf_shortcode');
/*
shortcode
[related_post_acf field="post_object_field_name" related_field="field_name_on_other_post"]
*/
Lawd have mercy! It works, thank you! Do you have any tips for a breakdown of the code syntax?
You must be logged in to reply to this topic.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.