Current status: Solved. . Done
Relationship Field throws error when related item is trashed
  • When you trash a related item and then go to a post with the Relationship Field it will throw an error because it can no longer find the related post. To fix this I added an if statement on line 281 of relationship.php and wrapped the code in it.
    if( $field['value'] )
    {
    foreach( $field['value'] as $key => $post )
    {
    if( is_object($post) ) {
    // right aligned info
    $title = '<span class="info">';
    $title .= $post->post_type;
    // WPML
    if( defined('ICL_LANGUAGE_CODE') )
    {
    $title .= ' (' . ICL_LANGUAGE_CODE . ')';
    }
    $title .= '</span>';
    // find title. Could use get_the_title, but that uses get_post(), so I think this uses less Memory
    $title .= apply_filters( 'the_title', $post->post_title, $post->ID );
    // status
    if($post->post_status != "publish")
    {
    $title .= " ($post->post_status)";
    }
    echo '<li>
    <a href="javascript:;" class="" data-post_id="'
    . $post->ID . '">' . $title . '<span class="remove"></span></a>
    <input type="hidden" name="'
    . $field['name'] . '[]" value="' . $post->ID . '" />
    </li>'
    ;
    }
    }
    }

    Also in get_value function I added an isset to the following:
    foreach( $value as $k => $v)
    {
    if( isset($ordered_posts[ $v ]) ) $value[ $k ] = $ordered_posts[ $v ];
    }
  • Thanks parapxl

    I'll add this validation in
  • Huh - from the ui for me if I create a relationship to a post, then delete that post and return to edit the original post that held the relationship -- in the ui for the relationship I see a '{}' for that deleted/null item.

    Null returns for that entry in the get_field('relationship') -- which is easy to check for - but the ui is the part that I'll hear it from a client.