{"id":6312,"date":"2016-04-05T17:13:20","date_gmt":"2016-04-05T21:13:20","guid":{"rendered":"https:\/\/themespress.ca\/?p=6312"},"modified":"2022-09-09T20:05:54","modified_gmt":"2022-09-10T00:05:54","slug":"redirect-the-single-post-in-the-search-condition","status":"publish","type":"post","link":"https:\/\/themespress.ca\/en\/redirect-the-single-post-in-the-search-condition\/","title":{"rendered":"Redirect the Single Post in the Search condition"},"content":{"rendered":"<div class=\"wpb-content-wrapper\"><div class=\"vc_row wpb_row vc_row-fluid\"><div class=\"wpb_column vc_column_container vc_col-sm-12\"><div class=\"vc_column-inner\"><div class=\"wpb_wrapper\">\n\t<div class=\"wpb_text_column wpb_content_element\" >\n\t\t<div class=\"wpb_wrapper\">\n\t\t\t<p>With this tutorial, we will enable a function that will allow you to redirect the Single Post if it&#8217;s the only search result with a Template Redirect filter.<\/p>\n<p>Inside the function, we will add a condition where if a post is the only result, it will redirect to the post&#8217;s permalink.<\/p>\n\n\t\t<\/div>\n\t<\/div>\n<h2 style=\"text-align: left\" class=\"vc_custom_heading vc_do_custom_heading\" >Redirect the Single Post in the Search condition<\/h2><div id=\"ts-enlighterjs-container-1069767\" class=\"ts-enlighterjs-container-single-enabled  \" style=\"width: 100%;  margin-top: 0px; margin-bottom: 0px;\" data-enlighter-doubleclick=\"true\" data-enlighter-windowbutton=\"true\" data-enlighter-windowtext=\"New Window\" data-enlighter-rawbutton=\"true\" data-enlighter-rawtext=\"RAW Code\" data-enlighter-infobutton=\"false\" data-enlighter-infotext=\"EnlighterJS\" data-enlighter-indent=\"2\"><pre id=\"ts-enlighterjs-pre-1069767\" class=\"\" style=\"white-space: pre-wrap; height: 100%; margin: 0; padding: 0;\" data-enlighter-language=\"standard\" data-enlighter-theme=\"enlighter\" data-enlighter-group=\"\" data-enlighter-title=\"\" data-enlighter-linenumbers=\"true\" data-enlighter-lineoffset=\"1\" data-enlighter-highlight=\"\">add_action(&#039;template_redirect&#039;, &#039;via_edirect_single_post&#039;); \nfunction via_redirect_single_post() { \n    if (is_search()) { \n\t\tglobal $wp_query; \n\t\tif ($wp_query-&gt;post_count == 1 &amp;&amp; $wp_query-&gt;max_num_pages == 1) { \n\t\t   wp_redirect( get_permalink( $wp_query-&gt;posts[&#039;0&#039;]-&gt;ID ) ); \n\t\t   exit; \n\t\t} \n\t} \n}<\/pre><\/div>\n\t<div class=\"wpb_text_column wpb_content_element\" >\n\t\t<div class=\"wpb_wrapper\">\n\t\t\t<p>Visit the <a href=\"https:\/\/codex.wordpress.org\/Plugin_API\/Action_Reference\/template_redirect\" target=\"_blank\" rel=\"noopener\">Template Redirect<\/a> page.<\/p>\n\n\t\t<\/div>\n\t<\/div>\n<\/div><\/div><\/div><\/div>\n<\/div>","protected":false},"excerpt":{"rendered":"With this tutorial, we will enable a function that will allow you to redirect the Single Post if it's the only search result with a Template Redirect filter. Inside the<div class=\"read-more\"><a class=\"btn button-secondary\" href=\"https:\/\/themespress.ca\/en\/redirect-the-single-post-in-the-search-condition\/\">Read More<\/a><\/div>","protected":false},"author":81,"featured_media":4525,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[283],"tags":[318],"class_list":["post-6312","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tutorials","tag-functions"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/themespress.ca\/en\/wp-json\/wp\/v2\/posts\/6312","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/themespress.ca\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/themespress.ca\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/themespress.ca\/en\/wp-json\/wp\/v2\/users\/81"}],"replies":[{"embeddable":true,"href":"https:\/\/themespress.ca\/en\/wp-json\/wp\/v2\/comments?post=6312"}],"version-history":[{"count":0,"href":"https:\/\/themespress.ca\/en\/wp-json\/wp\/v2\/posts\/6312\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/themespress.ca\/en\/wp-json\/wp\/v2\/media\/4525"}],"wp:attachment":[{"href":"https:\/\/themespress.ca\/en\/wp-json\/wp\/v2\/media?parent=6312"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/themespress.ca\/en\/wp-json\/wp\/v2\/categories?post=6312"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/themespress.ca\/en\/wp-json\/wp\/v2\/tags?post=6312"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}