WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm automatically generating links to (supposed) pages/posts.

These links are generated by get_permalink( $post->ID ), so in_theory, should be valid links. Unfortunately, in some situations, where the $post is actually a nav menu item, there is a "theoretical" permalink that actually doesn't correspond to an actual page.

So, sometimes, the page the link points to doesn't actually exist. I would like to be able to detect this condition (ie: broken link) prior to outputting the HTML for that link, perhaps substituting it for something else.

I don't want to open a separate HTTP request or socket to do this because that would slow down page load times while waiting for the response. I'd like to leverage WordPress' built-in rewrite logic to determine what the link would be and whether that corresponds to any page/post record in the DB. WordPress does this automatically already when it pulls up the correct page, so in theory this should be possible.

My question really is: are there any functions or methods of WP_Rewrite that already facilitate this, or do I need to roll my own code?

share|improve this question

You could call get_post_type with the id to check its type before outputting the permalink.

share|improve this answer

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.