title: I need mysql query for wordpress for get ordered list
body: <p>I need to get ordered list by three fields.</p>
  
  <pre><code>$sql = "
  SELECT 
      SQL_CALC_FOUND_ROWS $wpdb-&gt;posts.ID 
  FROM 
      $wpdb-&gt;posts 
  INNER JOIN 
      $wpdb-&gt;postmeta ON ( $wpdb-&gt;posts.ID = $wpdb-&gt;postmeta.post_id )
  INNER JOIN 
      $wpdb-&gt;postmeta AS mt1 ON ( $wpdb-&gt;posts.ID = mt1.post_id ) 
  
  WHERE 
      1=1
  
  AND 
  ( 
     ( mt1.meta_key = 'pub_series' AND mt1.meta_value = 'Book' ) 
  OR ( mt1.meta_key = 'pub_series' AND mt1.meta_value = 'Book chapter' ) 
  OR ( mt1.meta_key = 'pub_series' AND mt1.meta_value = 'Journal Article' )
  )
  
  /*AND 
  ( 
     mt3.meta_key = 'forthcoming'
  )*/
  
  AND 
      $wpdb-&gt;postmeta.meta_key = 'pub_year'        
  AND 
      $wpdb-&gt;posts.post_type = 'publication' 
  AND 
      $wpdb-&gt;posts.post_status = 'publish' 
  GROUP BY 
      $wpdb-&gt;posts.ID 
  
  ORDER BY 
  
      /*FIELD(mt3.meta_key, 'forthcoming') DESC,
      FIELD(mt3.meta_value, 'null') DESC,*/
      //FIELD($wpdb-&gt;postmeta.meta_value,1),
  
      $wpdb-&gt;postmeta.meta_value DESC, 
      $wpdb-&gt;posts.post_date DESC 
  ";
  
  $total = count($wpdb-&gt;get_results($sql));
  $offset = ( $paged * $posts_per_page ) - $posts_per_page;
  
  $results = $wpdb-&gt;get_results( $sql . "
      LIMIT 
          $offset, $posts_per_page" );
  </code></pre>
  
  <p>This is query ordered list by two fields: [custom_field] pub_year (possible values 2018, 2017...) and [delautl wp post date field] post_date. It is ok.</p>
  
  <p>But now I need to show items with [custom_field] forthcoming = 1. Problem that there three states if this field:
   - items with forthcoming = 1
   - items with forthcoming = 0
   - and items where custom field forthcoming not exist</p>
  
  <p>I need to get ordered list with firts items forthcoming = 1 and other should be ordered by pub_year and date. How I can do this. Tell me if you need some more info. Thanks a lot.</p>