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 would like to check if the tag name is the title of a post. And load that post page instead of a tag page.

I have post_type "Cities" and suppose that users will tag normal posts with city names. And when user selects a tag, I would like to check if a "Cities" post exists with this tag name. And show this post if it does.

I am thinking of redirecting from tag page to post page, but don't know when and how to run the check. Is it the right way? Can anyone help?

share|improve this question
Not sure why the question was downvoted. Anyway, is the tag also a custom taxonomy or wordpress default tag? – Sisir Dec 21 '11 at 20:06
Thanks, @Sisir. By tag I mean the default wp tag. – Slava Dec 22 '11 at 8:37
up vote 0 down vote accepted

Try the code from this article: http://shailan.com/2246/how-to-redirect-to-single-post-page-if-there-is-one-post-in-categorytag/

It checks $wp_query->post_count and redirects to the only article of an archive if there is just one.

share|improve this answer
Welcome to WordPress Stack Exchange, Waleri! Because this was your first answer I translated it to English. Please don’t use any other language here – otherwise, it would be too hard for other readers to understand what you have written. Also, always add an explanation to your answer, don’t post just links. Thanks! – toscho Mar 12 '12 at 19:17

You can use get_page_by_title() function to obtain the post object that has the same title. Link to the codex: http://codex.wordpress.org/Function_Reference/get_page_by_title. Here is the code you should place before get_header() in tags.php. It will help you redirect to the single post (or any other CPT if you're using that) if found with the same title.

$current_post = get_page_by_title(single_tag_title(), OBJECT, "post");
if($current_post !== null) {
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.