As @krzysiek wrote, it's not possible.
Simply put, you can't get the title of something that doesn't exists.
However, you may try to guess the title from the url.
Below is an excerpt from a code I wrote that works for both 404 and search pages (that's why I also check for the
$search_term = substr($_SERVER['REQUEST_URI'], 1);
$search_term = urldecode(stripslashes($search_term));
$search_term = rtrim($search_term, "/");
$find = array("'.html'", "'.+/'", "'[-/_]'");
$replace = " ";
$search_term = trim(preg_replace($find, $replace, $search_term));
$search_term = str_replace("%20", $replace, $search_term);
$search_term = preg_replace('!\s+!', ' ', $search_term);
Here you can use $search_term
as a guessed title.
You can use this string to search suggestions with something like that (better if you use some plugin such relevanssi to improve searches):
if (trim($search_term) == '') $search_term = get_search_query();
if (trim($search_term) == '') return false;
$s = str_replace("-", " ", $search_term);
$search_terms = explode(' ', $s);
$query = new WP_Query(array('post_type' => 'any', 's' => $s));
$posts = $query->posts;
if (count($posts) == 0) {
$query = new WP_Query(array(array('post_type' => 'any', 'name' => $search_term)));
$posts = $query->posts;
}
//... handle your results
You can even further improve this looking of occurrences of each word in post type names, taxonomies, etc, and providing links to correspondent archive pages (using, for instance, all items in $search_terms
).
Keep this code as an example, as it is the short version of a more complex function I use to handle 404 and search results.