title: WordPress custom query for multiple meta key and meta value for search
body: <p>I have Three DropDown with multi-selection. Also used AJAX for getting the data using WP_Query.</p>
  
  <p>Process: If I am select first OR any DropDown from the page then it will return a result. Then select the second DropDown then it will set the "AND" relation to getting the result means common result should return.</p>
  
  <p>I have faced an issue with setting the relation between different selections.</p>
  
  <p>Please help me!</p>
  
  <p>Here is the code: Filter.js</p>
  
  <pre><code>    var test1 = [];
      var test2 = [];
      var test3 = [];
  
  var ajaxurl = ajaxfilter.ajax_url;
  jQuery(document).ready(function() {
  
      jQuery('#domaine').on('change', function() {
          test1 = jQuery(this).val();
  
          if (test1 !== null) {
              var data = {
                  action: 'get_fill_portfolio',
                  opt_domaine: test1,
                  opt_status: test2,
                  opt_fond: test3
              }
              jQuery.post(ajaxurl, data, function(response) {
                  jQuery('.load-state').html(response);
              });
          }
  
      });
  
      jQuery('#status').on('change', function() {
          test2 = jQuery(this).val();
  
  
          if (test2 !== null) {
              var data = {
                  action: 'get_fill_portfolio',
                  opt_domaine: test1,
                  opt_status: test2,
                  opt_fond: test3
  
              }
              jQuery.post(ajaxurl, data, function(response) {
                  jQuery('.load-state').html(response);
              });
          }
  
      });
  
      jQuery('#fond').on('change', function() {
  
          test3 = jQuery(this).val();
  
  
          if (test3 !== null) {
              var data = {
                  action: 'get_fill_portfolio',
                  opt_domaine: test1,
                  opt_status: test2,
                  opt_fond: test3
  
              }
              jQuery.post(ajaxurl, data, function(response) {
                  jQuery('.load-state').html(response);
  
              });
          }
  
      });
  </code></pre>
  
  <p>Functions.php</p>
  
  <pre><code>add_action('wp_ajax_get_fill_portfolio', 'get_fill_portfolio');
  add_action('wp_ajax_nopriv_get_fill_portfolio', 'get_fill_portfolio');
  // Select Dropdown List
  function get_fill_portfolio() 
  {   
  
     /* check_ajax_referer( 'load_states', 'security' );
  
      $return = array();
      echo $return;
      wp_die(); // You missed this too*/
  
      $all_portfolio = [];
      global $post;
  
      $opt_domaine = $_POST['opt_domaine'];
      $opt_status = $_POST['opt_status'];
      $opt_fond = $_POST['opt_fond'];
  
  
      $string = rtrim(implode(',', $opt_domaine), ',');
      $string1 = rtrim(implode(',', $opt_status), ',');
      $string2 = rtrim(implode(',', $opt_fond), ',');
  
  
      $args = array(
          'post_type' =&gt; 'stm_portfolio',
          'post_status'    =&gt; 'publish',
          'orderby'        =&gt; 'menu_order',
          'order'          =&gt; 'ASC',
          'posts_per_page' =&gt; -1,
          'meta_query' =&gt; array(
              'relation' =&gt; 'OR',
  
             array(
                'relation' =&gt; 'AND',
                'mnf_domaine' =&gt;  array(
                  'key' =&gt; 'mnf_domaine',
                  'value' =&gt; $string,
                  'compare' =&gt; 'IN'
                  ),
              ),
  
              array(
                  'relation' =&gt; 'AND',
              'mnf_status' =&gt; array(
                  'key' =&gt; 'mnf_status',
                  'value' =&gt; $string1,
                  'compare' =&gt; 'IN'
                  ),
              ),  
  
            array(
                  'relation' =&gt; 'AND',
                  'mnf_fond' =&gt; array(
                  'key' =&gt; 'mnf_fond',
                  'value' =&gt; $string2,
                  'compare' =&gt; 'IN'
                  )
               )
              )
      );
  
  $query_s = new WP_Query( $args );
  
      if ( $query_s-&gt;have_posts() ) { 
  
          while ( $query_s-&gt;have_posts() ) : $query_s-&gt;the_post();
              $all_portfolio[] = [
                  'name' =&gt; get_the_title( $post-&gt;ID),
                  'mnf_site_web'  =&gt; get_field('mnf_site_web', $post-&gt;ID),
                  'link'   =&gt; get_permalink($post-&gt;ID),
                  'mnf_domaine'  =&gt; get_field('mnf_domaine', $post-&gt;ID),
                  'mnf_status'  =&gt; get_field('mnf_status', $post-&gt;ID),
                  'mnf_fond'  =&gt; get_field('mnf_fond', $post-&gt;ID),
                  'img_url'  =&gt; get_the_post_thumbnail_url( $post-&gt;ID, 'full' )
              ];
          endwhile;
  
          if ( $all_portfolio ) : 
              $domaine = get_field_object('mnf_domaine'); 
              $status = get_field_object('mnf_status'); 
              $fond = get_field_object('mnf_fond'); 
  
          endif;  
  
  ?&gt;
  
   &lt;div class="logo-filter"&gt;
          &lt;div class="row"&gt;
              &lt;?php foreach ( $all_portfolio as $portfolio ) : ?&gt;
                      &lt;div class="col-md-3 col-sm-4 col-xs-6"&gt;
                      &lt;a href="&lt;?=$portfolio['link']?&gt;" target="" class="logo-link"&gt;
                          &lt;img alt="&lt;?=$portfolio['name']?&gt;" src="&lt;?=$portfolio['img_url']?&gt;"&gt;
                      &lt;/a&gt;    
                      &lt;/div&gt;
              &lt;?php endforeach; ?&gt;
          &lt;/div&gt;
      &lt;/div&gt;
  &lt;?php
          wp_reset_query();   
          }
      }
  
  ?&gt;
  </code></pre>
  
  <p>It should be give result when I am searching from any dropdown with multiple selection.</p>
  
  <p>Currently, I have three Drop-Down. So here, I want to implement search functionality as per multi-selection from Drop-Down. When I use <code>'relation' =&gt; 'AND'</code> Then i have to select three Drop-Down and then it is return results. So i have tried to use the <code>'relation' =&gt; 'OR'</code> So it is return all result.</p>
  
  <p>So here i am confuse for using Relation between Three Drop-Down with Multi-Selection.</p>
  
  <p>I want to set Query like </p>
  
  <pre><code>AND ( 
    ( wp_postmeta.meta_key = 'mnf_domaine' AND wp_postmeta.meta_value IN ('media', 'media1') ) 
    AND
    ( mt1.meta_key = 'mnf_status' AND mt1.meta_value IN ('test1', 'test2') ) 
    OR 
    ( mt2.meta_key = 'mnf_fond' AND mt2.meta_value IN ('') )
  </code></pre>
  
  <p>Thank You!</p>