Support

Account

Home Forums ACF PRO How do I display a custom field in another custom post type as a shortcode? Reply To: How do I display a custom field in another custom post type as a shortcode?

  • 
    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"]
    */