title: Wordpress search result show by priority(first show posts by title)
body: <blockquote>
  <p>I have implemented few search filters for custom search. So now i have
  search functions that gets posts by title and custom field. But the
  returned result is show random.</p>
  <p>I want to have first shown search result by title, and then by custom
  field. <a href="http://www.kookse.tv/?s=aardappel" rel="nofollow noreferrer">Search result</a></p>
  <p>Is there a way to do that?</p>
  <p>The search works great, need just to display them by priority. Here is
  my code for search.</p>
  </blockquote>
  <pre><code>add_filter('posts_orderby','my_sort_custom',10,2);
          function my_sort_custom( $orderby, $query ){
              global $wpdb;
          
                  if(is_search()){
                  
                      $orderby =  $wpdb-&gt;prefix.&quot;posts.post_type DESC, {$wpdb-&gt;prefix}posts.ID ASC&quot;;
              }
              return  $orderby;
          }
  
   function __search_by_title_only( $search, &amp;$wp_query )
  {
      global $wpdb;
  
      if ( empty( $search ) )
          return $search; // skip processing - no search term in query
  
      $q = $wp_query-&gt;query_vars;    
      $n = ! empty( $q['exact'] ) ? '' : '%';
  
      $search =
      $searchand = '';
  
      foreach ( (array) $q['search_terms'] as $term ) {
          $term = esc_sql( like_escape( $term ) );
          $search .= &quot;{$searchand}($wpdb-&gt;posts.post_title LIKE '{$n}{$term}{$n}') &quot;;
          //$searchand = ' AND ';
      }
  
      if ( ! empty( $search ) ) {
          $search = &quot; AND ({$search}) &quot;;
          if ( ! is_user_logged_in() )
              $search .= &quot; AND ($wpdb-&gt;posts.post_password = '') &quot;;
      }
  
      return $search;
  }
  add_filter( 'posts_search', '__search_by_title_only', 1, 2 );
  
  
  
  
   function posts_join_post_meta( $join, $query ){
   
      global $wpdb;
      
      if( is_search()){
   
          $join .= &quot;
          INNER JOIN
            {$wpdb-&gt;postmeta} ON {$wpdb-&gt;posts}.ID = {$wpdb-&gt;postmeta}.post_id&quot;;
   
      }
   
      return $join;
   
  }
  add_filter( 'posts_join', 'posts_join_post_meta', 2, 2 );
  
  
  
  
  function recipe_search_where( $where, $query ){
   
      global $wpdb;
      if(is_search() ){
          $where .= &quot; OR (
          {$wpdb-&gt;postmeta}.meta_key = '_recipe_incredients' AND {$wpdb-&gt;postmeta}.meta_value LIKE ('%&quot;.$wpdb-&gt;escape( get_query_var('s') ).&quot;%') ) &quot;;
      }
      return $where;
  }
  add_filter( 'posts_where', 'recipe_search_where', 10, 2 );
  </code></pre>