Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

My users can link to things that don't exist on the page. That's something I chose because it is the best option for my concept. Anyway, what I want to do is this... Let's say I clicked on a link that should link to the post "Hello World", but as said, this post does not exist...

What I want to do is show this post_title (Hello World) on the 404 Error page and make suggestions as context related posts. The related posts part is not a problem, but how do I get the clicked link / post title on the 404 page?

share|improve this question
add comment

2 Answers

up vote 0 down vote accepted

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.

share|improve this answer
Thanks Andrea, that's a very nice answer. Is there any chance that I can get the full code of this one? It's very important for my site, because I have lots of 404 pages that I want to improve... That would be very very nice. (PS: you don't have to put it here, it can via Skype etc.) –  user1627363 Jul 2 '13 at 11:56
Indeed, this is not the right place, I'm afraid. Please complete your profile and we can talk about it privately. –  Andrea Sciamanna Jul 2 '13 at 12:06
Exactly. Well, I completed my profile. You can contact me via Skype. –  user1627363 Jul 2 '13 at 12:29
PS: or email me. –  user1627363 Jul 2 '13 at 12:43
add comment

I'm afraid it's not possible.

You are unable to check what was the link title, that user clicked. The only information you can get, is the URL (href attribute) it was pointing to (but it's different than the title of that link).

share|improve this answer
Too bad. The SEO of the site will be truly afwul then. Thanks for the answer! Let's see if someone else can give an answer too. Otherwise I will accept yours. –  user1627363 Jul 2 '13 at 11:28
add comment

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.