title: How to display custom search results from a selected category of a custom post type?
body: <p>I have a website that has a blog and a custom post type called <code>proyecto</code>.</p>
  
  <p>They are on <code>mywebpage.com/blog</code> and <code>mywebpage.com/portafolio</code>.Each of those pages have a search field and category dropdown selector.</p>
  
  <p>Each page should <strong>only</strong> search and filter for their respective posts. Also, each of them has a custom search result layout. If I'm on <code>mywebpage.com/portafolio</code> the search results should only display <code>proyecto</code> post type results with its custom layout. The same goes for when I only select a category from the dropdown (and dont type anything), it should only display posts type <code>proyecto</code> that have the category selected and only show the categories assigned to <code>proyecto</code> post types, again, with its own layout.</p>
  
  <p>At the moment the search and category filter work (Im not even sure if it works properly) but it merges both of the results (or doesnt show them all) and only displays them with the <code>post</code> design layout... and when I look for an empty seach,it always displays all of the <code>proyecto</code> posts and some of the <code>post</code> results with the <code>proyecto</code> layout.</p>
  
  <p>Here is my code on both <code>page-blog.php</code> and <code>page-proyecto.php</code>:</p>
  
  <pre><code>&lt;section id="search"&gt;
      &lt;div class="container"&gt;
          &lt;div class="page-title"&gt;PAGE TITLE&lt;/div&gt;
          &lt;form action="" method="post" class="search-form searchandfilter justify-content-between" id="searchbar"&gt;
              &lt;div class="row"&gt;
                  &lt;div class="offset-lg-2 col-lg-6"&gt;
                      &lt;label class="search-bar"&gt;
                          &lt;div class="btn-search"&gt;&lt;i class="fa fa-search"&gt;&lt;/i&gt;&lt;/div&gt;
                          &lt;input type="search" name="ofsearch" class="search-txt" placeholder="&lt;?php echo esc_attr_x( 'Búsqueda', 'placeholder' ) ?&gt;" value="&lt;?php echo get_search_query()?&gt;" /&gt;
                          &lt;input type="hidden" name="post_type" value="post_type"/&gt;
                      &lt;/label&gt;
                  &lt;/div&gt;
                  &lt;div class="col-lg-4"&gt;
                      &lt;?php
                      $categories = get_categories( array ('orderby' =&gt; 'name', 'order' =&gt; 'asc' ) );
                      if( $terms = get_categories( array('taxonomy' =&gt; 'category', 'orderby' =&gt; 'name') ) )
                      {
                          ?&gt;
                          &lt;select name="ofcategory" id="ofcategory" class="postform custom-select" name="categoryfilter"&gt;
                              &lt;option value="0"&gt;Categor&amp;iacute;a&lt;/option&gt;
                              &lt;?php
                              foreach ( $terms as $term )
                              {
                                  ?&gt;
                                  &lt;option value="&lt;?php echo $term-&gt;term_id;?&gt;" data-categoria="&lt;?php echo $term-&gt;name;?&gt;"&gt;&lt;?php echo $term-&gt;name;?&gt;&lt;/option&gt;
                                  &lt;?php
                              }
                              ?&gt;
                          &lt;/select&gt;
                          &lt;?php
                      }
                      ?&gt;
                      &lt;input type="hidden" name="ofcategory_operator" value="and"&gt;
                  &lt;/div&gt;
              &lt;/div&gt;
              &lt;div class="row justify-content-end"&gt;
                  &lt;input type="hidden" name="ofsubmitted" value="1"&gt;
                  &lt;input type="submit" value="Filtrar" class="btn-submit btn-main-aqua"&gt;
                  &lt;div class="btn-submit btn-main-aqua" onclick="displaymessage();reset()"&gt;Vaciar Campos&lt;/div&gt;
              &lt;/div&gt;
          &lt;/form&gt;
      &lt;/div&gt;
  &lt;/section&gt;
  </code></pre>
  
  <p><strong>NOTE</strong>:</p>
  
  <ul>
  <li>This is the same code I use on both pages.</li>
  <li>I only change the <em>page title</em> and <em>value</em> of the hidden input in the search.</li>
  <li>I cannot know what the categories' names will be, they will be created by the user.</li>
  </ul>
  
  <p>Here is my code on both <code>search.php</code> and <code>category.php</code>:</p>
  
  <pre><code>&lt;?php get_header();?&gt;
      &lt;section id="search"&gt;
          &lt;div class="container"&gt;
              &lt;div class="row m-0"&gt;&lt;div class="page-title"&gt;Resultados de b&amp;uacute;squeda&lt;/div&gt;&lt;/div&gt;
              &lt;div class="row p-3"&gt;&lt;div class="title"&gt;"&lt;?php the_search_query();?&gt;"&lt;/div&gt;&lt;/div&gt;
  
              &lt;div class="row row-eq-height m-0"&gt;
                  &lt;?php
  
                  if(have_posts())
                  {
                      if (get_post_type() == 'proyecto')
                      {
                          while (have_posts())
                          {
                              the_post();
                              $img = get_field("port-logo");
                              ?&gt;
                              &lt;div class="col-lg-4 col-md-6 col-sm-12 p-0" data-aos="fade-up" data-aos-duration="1000"&gt;
                                  &lt;a href="&lt;?php the_permalink();?&gt;"&gt;
                                      &lt;div class="proyecto-img"&gt;
                                          &lt;img src="&lt;?php echo $img;?&gt;"&gt;
                                      &lt;/div&gt;
                                  &lt;/a&gt;
                              &lt;/div&gt;&lt;!--  ITEM    --&gt;
                              &lt;?php
                          }
                      }
                      else
                      {
                          if (get_post_type() == 'post')
                          {
                              while (have_posts())
                              {
                                  the_post();
                                  $titulo = get_field("blog-titulo");
                                  $subtitulo = get_field("blog-subtitle");
                                  $img = get_field("blog-img");
                                  $intro = get_field("blog-intro");
                                  ?&gt;
                                  &lt;div class="col-lg-4 col-md-6 col-sm-12" data-aos="fade-up" data-aos-duration="1000"&gt;
                                      &lt;div class="post-cont"&gt;
                                          &lt;a href="&lt;?php echo the_permalink();?&gt;"&gt;
                                              &lt;div class="post-img"&gt;
                                                  &lt;img src="&lt;?php echo $img;?&gt;"&gt;
                                                  &lt;div class="subtitle"&gt;&lt;?php foreach((get_the_category()) as $category){ echo '- ' . $category-&gt;cat_name; }?&gt;&lt;/div&gt;
                                              &lt;/div&gt;
                                          &lt;/a&gt;
                                          &lt;div class="desc-title"&gt;&lt;?php echo the_title();?&gt;&lt;/div&gt;
                                          &lt;div class="post-desc"&gt;&lt;?php echo $intro;?&gt;&lt;/div&gt;
                                          &lt;a href="&lt;?php echo the_permalink();?&gt;"&gt;Leer m&amp;aacute;s&lt;/a&gt;
                                      &lt;/div&gt;
                                  &lt;/div&gt;&lt;!--  ITEM    --&gt;
                                  &lt;?php
                              }
                          }
                          else
                          {
                              ?&gt;
                              No results.
                              &lt;?php
                          }
                      }
                  }
                  else
                  {
                      ?&gt;
                      No results.
                      &lt;?php
                  }
                  ?&gt;
              &lt;/div&gt;
              &lt;div class="row p-0 m-0 mt-5"&gt;
                  &lt;a href="javascript:history.go(-1)"&gt;&lt;div class="btn-main-red"&gt;Regresar&lt;/div&gt;&lt;/a&gt;
              &lt;/div&gt;
          &lt;/div&gt;
      &lt;/section&gt;
  &lt;?php get_footer();?&gt;
  </code></pre>
  
  <p>Im not sure if this is the correct way to code it or if I should have 2 different search results documents for that... </p>
  
  <p>My main problem is that they function as if they were 1 same search form instead of 2 different (because that code is on each page with different values on the hidden field), same goes to the <code>search.php</code>. Should I make 2 different files, if so how should i o it?</p>
  
  <p>When I filter by category, I would like the url outcome to be something like this:</p>
  
  <p>for the custom post type: <code>mywebpage.com/proyecto/category/category1/?s=some_text</code></p>
  
  <p>and for blog: <code>mywebpage.com/category/category1/?s=some_text</code></p>
  
  <p>I also tried using POST and GET and it did work but it makes my categories stop working...</p>
  
  <p>I have been looking arround for an answer to my problem for days and even searched in the codex and various forums but I have not been successful, Im also a begginer so I dont know if I may have found the answer but I just didnt understand it... Any form of help adn guidance is apreciated, thanks.</p>