{"id":6338,"date":"2016-03-07T14:55:26","date_gmt":"2016-03-07T19:55:26","guid":{"rendered":"https:\/\/themespress.ca\/?p=6338"},"modified":"2022-09-10T15:12:41","modified_gmt":"2022-09-10T19:12:41","slug":"search-post-type-query","status":"publish","type":"post","link":"https:\/\/themespress.ca\/en\/search-post-type-query\/","title":{"rendered":"Search Post Type Query"},"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>In a previous tutorial, <a href=\"https:\/\/themespress.ca\/le-search-wordpress\/\" target=\"_blank\" rel=\"noopener\">WordPress Search<\/a>, we saw how to filter post types with WordPress&#8217; search function. This tutorial will allow you to customize the search even more by creating a search post type for posts.<\/p>\n<p>The first thing we need to do is create our Post Type form. The value needs to be the name of the Post Type. In our example, the post type is <strong>viavideos<\/strong>.<\/p>\n\n\t\t<\/div>\n\t<\/div>\n<h2 style=\"text-align: left\" class=\"vc_custom_heading vc_do_custom_heading\" >Search Post Type<\/h2><div id=\"ts-enlighterjs-container-2656746\" 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-2656746\" 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=\"\">&lt;form class=&quot;search-form&quot; role=&quot;search&quot; action=&quot;&lt;?php echo home_url( &#039;\/&#039; ); ?&gt;&quot; method=&quot;get&quot;&gt;\n&lt;label&gt; \n&lt;input class=&quot;search-field&quot; title=&quot;&lt;?php echo esc_attr_x( &#039;Rechercher :&#039;, &#039;label&#039; ) ?&gt;&quot; name=&quot;s&quot; type=&quot;search&quot; value=&quot;&lt;?php echo get_search_query() ?&gt;&quot; placeholder=&quot;&lt;?php echo esc_attr_x( &#039;Rechercher &hellip;&#039;, &#039;placeholder&#039; ) ?&gt;&quot; \/&gt; \n&lt;input name=&quot;post_type&quot; type=&quot;hidden&quot; value=&quot;viavideos&quot; \/&gt; \n&lt;\/label&gt; \n&lt;input class=&quot;search-submit&quot; type=&quot;submit&quot; value=&quot;&lt;?php echo esc_attr_x( &#039;Rechercher&#039;, &#039;submit button&#039; ) ?&gt;&quot; \/&gt;\n&lt;\/form&gt;<\/pre><\/div>\n\t<div class=\"wpb_text_column wpb_content_element\" >\n\t\t<div class=\"wpb_wrapper\">\n\t\t\t<p>Afterwards, we need to create the <strong>pre_get_post<\/strong> that will allow us to display the results of the Post type with the condition of search when you&#8217;re not an admin user. (<strong>Replace viavideos par votre Post Type<\/strong>).<\/p>\n\n\t\t<\/div>\n\t<\/div>\n<div id=\"ts-enlighterjs-container-7619701\" 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-7619701\" 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=\"\">function searchfilterquery($query) {\nif ($query-&gt;is_search &amp;&amp; !is_admin() ) {\n\tif(isset($_GET[&#039;post_type&#039;])) {\n\t\t$type = $_GET[&#039;post_type&#039;];\n\t\tif($type == &#039;viavideos&#039;) {\n\t\t   $query-&gt;set(&#039;post_type&#039;,array(&#039;viavideos&#039;));\n\t\t}\n\t}\n}\nreturn $query;\n}\nadd_filter(&#039;pre_get_posts&#039;,&#039;searchfilterquery&#039;);<\/pre><\/div>\n\t<div class=\"wpb_text_column wpb_content_element\" >\n\t\t<div class=\"wpb_wrapper\">\n\t\t\t<p>We need to open the <strong>search.php<\/strong> file and add a new condition and add this new condition in your content&#8217;s loop in the <strong>search.php<\/strong> template.<\/p>\n<p>Notice the results. We tell the <strong>search.php<\/strong> that if there&#8217;s a <strong>viavideos<\/strong> post type query, we need call the <strong>content-videos.php<\/strong> template. Or if it&#8217;s a post query, then the <strong>content-posts.php<\/strong> content Post template will be called. Otherwise, there will be no results.<\/p>\n\n\t\t<\/div>\n\t<\/div>\n<div id=\"ts-enlighterjs-container-1887739\" 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-1887739\" 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=\"\">&lt;?php if(isset($_GET[&#039;post_type&#039;])) { $type = $_GET[&#039;post_type&#039;]; if($type == &#039;viavideos&#039;) {?&gt;\n&lt;?php get_template_part( &#039;content&#039;, &#039;videos&#039; ); ?&gt;\n\n&lt;?php } else { ?&gt;\n&lt;?php get_template_part( &#039;content&#039;, &#039;posts&#039; ); ?&gt;\n&lt;?php } ?&gt;\n\n&lt;?php } else { ?&gt;\n&lt;?php _e( &#039;Pas de r&eacute;sultats&#039; ); ?&gt;\n&lt;?php } ?&gt;<\/pre><\/div>\n\t<div class=\"wpb_text_column wpb_content_element\" >\n\t\t<div class=\"wpb_wrapper\">\n\t\t\t<p>Simply try it out and test the results.<\/p>\n\n\t\t<\/div>\n\t<\/div>\n<\/div><\/div><\/div><\/div>\n<\/div>","protected":false},"excerpt":{"rendered":"In a previous tutorial, WordPress Search, we saw how to filter post types with WordPress' search function. This tutorial will allow you to customize the search even more by creating<div class=\"read-more\"><a class=\"btn button-secondary\" href=\"https:\/\/themespress.ca\/en\/search-post-type-query\/\">Read More<\/a><\/div>","protected":false},"author":81,"featured_media":4517,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[283],"tags":[318,346],"class_list":["post-6338","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tutorials","tag-functions","tag-search"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/themespress.ca\/en\/wp-json\/wp\/v2\/posts\/6338","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=6338"}],"version-history":[{"count":0,"href":"https:\/\/themespress.ca\/en\/wp-json\/wp\/v2\/posts\/6338\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/themespress.ca\/en\/wp-json\/wp\/v2\/media\/4517"}],"wp:attachment":[{"href":"https:\/\/themespress.ca\/en\/wp-json\/wp\/v2\/media?parent=6338"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/themespress.ca\/en\/wp-json\/wp\/v2\/categories?post=6338"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/themespress.ca\/en\/wp-json\/wp\/v2\/tags?post=6338"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}