0

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?

  • 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
0

Try the code from this article: http://metinsaylan.com/wordpress/2011/02/15/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.

  • 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! – fuxia Mar 12 '12 at 19:17
0

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) {
    wp_redirect($current_post->guid);
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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