title: WordPress search results ordered by custom post types are not grouped
body: <p>For a WordPress website I have a couple of custom post types, named 'cities' and 'books', and also the regular blog posts. I have customized the search results (only search in the post title) page so that the results are to be ordered by custom post type. But the problem is that the search results within a custom post type are not really grouped. Example: when I search for the letters 'am' i get the following results in the search results page:</p>
  
  <pre><code>BOOKS
  -----
  Book 1
  
  CITIES
  ------
  City 1
  
  BOOKS
  -----
  Book 2
  Book 3
  
  CITIES
  ------
  City 2
  City 3
  City 4
  
  BLOGPOSTS
  ---------
  Post 1
  Post 2
  Post 3
  etc.
  </code></pre>
  
  <p>What I would like:</p>
  
  <pre><code>BOOKS
  -----
  Book 1
  Book 2
  Book 3
  
  CITIES
  ------
  City 1
  City 2
  City 3
  City 4
  
  BLOGPOSTS
  ---------
  Post 1
  Post 2
  Post 3
  etc.
  </code></pre>
  
  <p>This is my loop:</p>
  
  <pre><code>&lt;?php if(have_posts()) : ?&gt;
  
                              &lt;?php   
                                  $last_type = "";
                                  $typecount = 0;
                                  while ( have_posts() ) : the_post();
                              ?&gt;
  
                                  &lt;?php
                                      if ($last_type != $post-&gt;post_type){
                                          $typecount = $typecount + 1;
                                          if ($typecount &gt; 1){
                                              echo '&lt;/div&gt;&lt;!-- close type-container --&gt;'; //close type container
                                          }
  
                                          // save the post type.
                                          $last_type = $post-&gt;post_type;
  
                                          //open type container
                                          switch ($post-&gt;post_type) {
                                              case 'books':
                                                  echo "&lt;div class=\"books-container\"&gt;&lt;h2&gt;";
                                                  _e('Books', 'qode');
                                                  echo "&lt;/h2&gt;";
                                              break;
  
                                              case 'cities':
                                                  echo "&lt;div class=\"cities-container\"&gt;&lt;h2&gt;";
                                                  _e('Destinations', 'qode');
                                                  echo "&lt;/h2&gt;";
                                              break;
  
                                              case 'post':
                                                  echo "&lt;div class=\"blog-container\"&gt;&lt;h2&gt;";
                                                  _e('Blogposts', 'qode');
                                                  echo "&lt;/h2&gt;";
                                              break;
                                          }
                                      }
                                  ?&gt;
  
                                  &lt;?php if('books' == get_post_type()) : ?&gt;
  
                                       &lt;?php get_template_part('templates/books_search', 'loop'); ?&gt;
  
                                  &lt;?php elseif('cities' == get_post_type()) : ?&gt;
  
                                      &lt;?php get_template_part('templates/cities_search', 'loop'); ?&gt;
  
                                  &lt;?php elseif('post' == get_post_type()) : ?&gt;
  
                                      &lt;?php get_template_part('templates/blog_search', 'loop'); ?&gt;
  
                                  &lt;?php endif; ?&gt;                     
  
                              &lt;?php endwhile; // endwhile have_posts ?&gt;
  
                              &lt;?php // Pagination ?&gt;
                              &lt;?php if($qode_options_proya['pagination'] != "0") : ?&gt;
                                  &lt;?php pagination($wp_query-&gt;max_num_pages, $blog_page_range, $paged); ?&gt;
                              &lt;?php endif; ?&gt;
  
                              &lt;?php else: //If no posts are present ?&gt;
                                  &lt;div class="entry nothing-found"&gt;                        
                                      &lt;h3&gt;&lt;?php _e('Sorry, there is no 100% ', 'qode') . " " . the_search_query() . " " . _e(' yet.') ?&gt;&lt;/h3&gt;
                                      &lt;p&gt;&lt;?php _e('But there are loads of other 100% destinations available. Try again in the searchfield below.', 'qode'); ?&gt; 
                                  &lt;/div&gt;
                          &lt;?php endif; // endif have_posts ?&gt;
  </code></pre>
  
  <p>And this is an example of the template file for a CPT that is being included:</p>
  
  <pre><code>&lt;div &lt;?php post_class('vc_span4 wpb_column column_container'); ?&gt; style="padding-bottom: 60px;"&gt;
  
  &lt;div class="wpb_wrapper book"&gt;
  
      &lt;?php // Get the post thumbnail ?&gt;
      &lt;div class="wpb_single_image wpb_content_element element_from_fade element_from_fade_on"&gt;
          &lt;div class="wpb_wrapper"&gt;
              &lt;?php 
                  if ( has_post_thumbnail() ) {
                      echo '&lt;a href="' . get_permalink( $thumbnail-&gt;ID ) . '" title="' . the_title_attribute( 'echo=0' ) . '" class="book-holder" &gt;&lt;span class="book-thumb"&gt;';
                      echo get_the_post_thumbnail( $post-&gt;ID, 'book-thumbnail' ); 
                      echo '&lt;/span&gt;&lt;/a&gt;';
                  }
              ?&gt;
          &lt;/div&gt; 
      &lt;/div&gt;
  
  &lt;/div&gt;
  
  &lt;div class="wpb_text_column wpb_content_element text-align-center" style="margin: 20px 0px 0px 0px; "&gt;
  
      &lt;div class="wpb_wrapper"&gt;
          &lt;h5&gt;&lt;?php the_title(); ?&gt;&lt;/h5&gt;
      &lt;/div&gt;
      &lt;a href="&lt;?php the_permalink(); ?&gt;" target="_blank" class="qbutton  small" style="margin: 20px 0px 0px 0px; "&gt;&lt;?php _e('More Info', 'qode'); ?&gt;&lt;/a&gt;
  
  &lt;/div&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>I must be doing something wrong here, but I just can't figure out what. Can someone please help me?</p>