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.

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
add comment

1 Answer

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

share|improve this answer
add comment

Your Answer

 
discard

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.