title: Wordpress query adds INNER/LEFT JOIN. Why or how to remove/prevent INNER/LEFT JOIN?
body: <p>I am working on a filter for posts in wordpress wich uses data from postsmeta:
  My date filter is working and gives me this query:</p>
  
  <p><strong>Working query result:</strong></p>
  
  <pre><code>SELECT SQL_CALC_FOUND_ROWS wp_posts.ID 
  FROM wp_posts 
  LEFT JOIN wp_postmeta ON (wp_posts.ID = wp_postmeta.post_id) 
  WHERE 1=1 AND YEAR(wp_posts.post_date)=2017 
  AND MONTH(wp_posts.post_date)=03 AND wp_posts.post_type = 'projects' 
  AND (wp_posts.post_status = 'publish' OR wp_posts.post_status = 'future' 
  OR wp_posts.post_status = 'draft' OR wp_posts.post_status = 'pending' 
  OR wp_posts.post_status = 'private') 
  GROUP BY wp_posts.ID 
  ORDER BY wp_posts.post_date DESC LIMIT 0, 20
  </code></pre>
  
  <blockquote>
    <p>When I filter by postmeta.meta_value I get an extra INNER JOIN which
    is causing issues:</p>
  </blockquote>
  
  <p><strong>Filter <em>(not exactly working)</em>:</strong></p>
  
  <pre><code>$query-&gt;set('meta_query', array(
     array(
        'key' =&gt; 'project_cust_id',
        'value' =&gt; $project_cust_id,
        'compare' =&gt; '=' 
     )
  ));
  </code></pre>
  
  <p><strong>Query <em>(not working result)</em>:</strong></p>
  
  <pre><code>SELECT SQL_CALC_FOUND_ROWS wp_posts.ID 
  FROM wp_posts 
  INNER JOIN wp_postmeta ON ( wp_posts.ID = wp_postmeta.post_id ) 
  LEFT JOIN wp_postmeta ON (wp_posts.ID = wp_postmeta.post_id) 
  WHERE 1=1 AND ( ( wp_postmeta.meta_key = 'project_cust_id' 
  AND wp_postmeta.meta_value = '12345' ) ) AND wp_posts.post_type = 'projects' 
  AND (wp_posts.post_status = 'publish' OR wp_posts.post_status = 'future' 
  OR wp_posts.post_status = 'draft' OR wp_posts.post_status = 'pending' 
  OR wp_posts.post_status = 'private') 
  GROUP BY wp_posts.ID 
  ORDER BY wp_posts.post_date DESC LIMIT 0, 20
  </code></pre>
  
  <p>When I query this straight into my DB I get no results and error below, but when I remove the extra line: <code>INNER JOIN wp_postmeta ON ( wp_posts.ID = wp_postmeta.post_id )</code> or when I remove the line: <code>LEFT JOIN wp_postmeta ON (wp_posts.ID = wp_postmeta.post_id)</code>  I do get the results as expected.</p>
  
  <blockquote>
    <p><strong>EDIT: MySQL returns error: Not unique value table/alias: 'wp_postmeta'</strong></p>
  </blockquote>
  
  <p>My question is:</p>
  
  <ul>
  <li>Why does my code adds another INNER JOIN? </li>
  <li>How can I remove/prevent this extra INNER JOIN or LEFT JOIN?</li>
  </ul>
  
  <p>Any help would be appreciated.</p>