Custom rewrite rule based on other custom post type title

So after 2 days of searching I found a solution.

Permalink manager lite + this function and all works fine.

//Rewrite rule 
function pm_extra_permastructure_tags($default_uri, $native_slug, $post, $slug, $native_uri) {
  // Do not affect native URIs
  if($native_uri == true || empty($post->post_type) || $post->post_type !== 'mieszkanie') { return $default_uri; }

    $inwestycja = get_field('inwestycja', $post->ID);

    if( !empty($inwestycja) ) {
        $inwestycjaObj = get_post($inwestycja);
        
        $default_uri = str_replace('%mieszkanie_slug%', $inwestycjaObj->post_name, $default_uri);
    }
  
  return $default_uri;
}
add_filter('permalink_manager_filter_default_post_uri', 'pm_extra_permastructure_tags', 3, 5);

error code: 523