title: Get WP posts based on multiple meta key/value pairs using IN
body: <p>I'm having some trouble formatting my query to fetch a list of posts from the database based on the value of two meta values. I'm using WordPress 4.9.4 and I need to use <code>$wpdb-&gt;get_results()</code> and <strong>not</strong> a normal <code>get_posts()</code> function call with arguments.</p>
  
  <p>Here is what I have so far, but if I try and include the <code>IN</code> part in the MySQL query, it doesn't return anything but it should return at least one post.</p>
  
  <pre class="lang-php prettyprint-override"><code>global $wpdb;
  
  $posts = $wpdb-&gt;get_results(
      $wpdb-&gt;prepare(
          'SELECT ' . $wpdb-&gt;prefix . 'posts.*, ' . $wpdb-&gt;prefix . 'postmeta.* FROM ' . $wpdb-&gt;prefix . 'posts, ' . $wpdb-&gt;prefix . 'postmeta WHERE ' . $wpdb-&gt;prefix . 'posts.post_type = %s AND ' . $wpdb-&gt;prefix . 'postmeta.post_id = ' . $wpdb-&gt;prefix . 'posts.ID AND (' . $wpdb-&gt;prefix . 'postmeta.meta_key = %s AND ' . $wpdb-&gt;prefix . 'postmeta.meta_value = %s) AND (' . $wpdb-&gt;prefix . 'postmeta.meta_key = %s AND ' . $wpdb-&gt;prefix . 'postmeta.meta_value IN (%s)) ORDER BY ' . $wpdb-&gt;prefix . 'posts.post_title ASC LIMIT %d OFFSET %d',
          'book',
          'book_genre',
          $genre,
          'category',
          '239, 440',
          $limit,
          $offset
      )
  );
  </code></pre>
  
  <p>The above query should get all posts where:</p>
  
  <ul>
  <li>the post has a <code>post_type</code> of <code>book</code> in <code>wp_posts</code></li>
  <li>has a meta_key of <code>book_genre</code> and meta_value of <code>fiction</code> in <code>wp_postmeta</code></li>
  <li>has a meta_key of <code>category</code> and meta_value of <code>239, 440</code> in <code>wp_postmeta</code></li>
  <li>limit results to <code>xyz</code></li>
  <li>offset results of <code>xyz</code></li>
  </ul>
  
  <p>However, the above query returns a value of <code>null</code> if I <code>var_dump( $posts );</code> onto the page, so I imagine that the in part is causing some sort of conflict with the first meta key/value rule, perhaps?</p>
  
  <p>If I remove the <code>IN</code> part of the query it gets everything as expected minus the filtering of the categories of course that I'd like. The category ids are an array of ids that are converted into a comma-separated string using PHP's <code>implode()</code>.</p>
  
  <p>For example:</p>
  
  <ul>
  <li><p>Post <code>1</code> has a post_type of <code>book</code>, has a meta key/value pair of <code>book_genre</code> and <code>fiction</code> and has a meta key/value pair of 'category' and <code>440</code> so <strong><em>should</em></strong> be included using the above query.</p></li>
  <li><p>Post <code>2</code> has a post_type of <code>book</code>, has a meta key/value pair of <code>book_genre</code> and <code>fiction</code> and has a meta key/value pair of 'category' and <code>323</code> so <strong><em>should not</em></strong> be included using the above query.</p></li>
  </ul>