title: Do a custom query search in wordpress by post meta
body: <p>I'm doing a custom search in wordpress, and I would like to find my custom type articles by searching them from custom meta keys.</p>
  
  <p>This code filters posts by custom types, but filters all of them by custom metas, showing no results!</p>
  
  <pre><code>function filtri_di_ricerca( $query ) {
      if ( is_admin() || ! $query-&gt;is_main_query() )
          return;
  
      if (is_home())
      {
          $query-&gt;set('post_type', 'post_annunci');  
          $query-&gt;set('posts_per_page', 1000);
  
          $meta_query = array();
  
          // Only check these form fields
          $fields = array( 'annuncio_contratto', 'annuncio_categorie', 'annuncio_tipologia', 'annuncio_citta', 'annuncio_prezzo');
          foreach( $fields as $field ) {
              if( $_GET[$field] != '' ) {
                  // We have something to match, otherwise ignore the field...
                  $meta_query[] = array(
                      'key' =&gt; $field,
                      'value' =&gt; $_GET[$field], 
                      'compare' =&gt; '='
                  );
              }
          }
          $meta_query['relation'] = 'AND';
  
          $query-&gt;set('meta_query',$meta_query);
      }
  
      return;
  }
  add_action( 'pre_get_posts', 'filtri_di_ricerca');
  </code></pre>
  
  <p>The search address is:</p>
  
  <pre><code>http://example.com/?annuncio_contratto=Vendita&amp;annuncio_categorie=Residenziale&amp;annuncio_tipologia=Appartamento&amp;annuncio_citta=NewYork
  </code></pre>
  
  <p>DEBUG with:</p>
  
  <pre><code>echo "&lt;pre&gt;"; print_r($wp_query-&gt;query_vars); echo "&lt;/pre&gt;";
  </code></pre>
  
  <p>result:</p>
  
  <pre><code>Array
  (
      [error] =&gt; 
      [m] =&gt; 0
      [p] =&gt; 0
      [post_parent] =&gt; 
      [subpost] =&gt; 
      [subpost_id] =&gt; 
      [attachment] =&gt; 
      [attachment_id] =&gt; 0
      [name] =&gt; 
      [static] =&gt; 
      [pagename] =&gt; 
      [page_id] =&gt; 0
      [second] =&gt; 
      [minute] =&gt; 
      [hour] =&gt; 
      [day] =&gt; 0
      [monthnum] =&gt; 0
      [year] =&gt; 0
      [w] =&gt; 0
      [category_name] =&gt; 
      [tag] =&gt; 
      [cat] =&gt; 
      [tag_id] =&gt; 
      [author_name] =&gt; 
      [feed] =&gt; 
      [tb] =&gt; 
      [paged] =&gt; 0
      [comments_popup] =&gt; 
      [meta_key] =&gt; 
      [meta_value] =&gt; 
      [preview] =&gt; 
      [s] =&gt; 
      [sentence] =&gt; 
      [fields] =&gt; 
      [menu_order] =&gt; 
      [category__in] =&gt; Array
          (
          )
  
      [category__not_in] =&gt; Array
          (
          )
  
      [category__and] =&gt; Array
          (
          )
  
      [post__in] =&gt; Array
          (
          )
  
      [post__not_in] =&gt; Array
          (
          )
  
      [tag__in] =&gt; Array
          (
          )
  
      [tag__not_in] =&gt; Array
          (
          )
  
      [tag__and] =&gt; Array
          (
          )
  
      [tag_slug__in] =&gt; Array
          (
          )
  
      [tag_slug__and] =&gt; Array
          (
          )
  
      [post_type] =&gt; post_annunci
      [posts_per_page] =&gt; 1000
      [meta_query] =&gt; Array
          (
              [0] =&gt; Array
                  (
                      [key] =&gt; annuncio_contratto
                      [value] =&gt; Vendita
                      [compare] =&gt; =
                  )
  
              [1] =&gt; Array
                  (
                      [key] =&gt; annuncio_categorie
                      [value] =&gt; Residenziale
                      [compare] =&gt; =
                  )
  
              [2] =&gt; Array
                  (
                      [key] =&gt; annuncio_tipologia
                      [value] =&gt; Appartamento
                      [compare] =&gt; =
                  )
  
              [3] =&gt; Array
                  (
                      [key] =&gt; annuncio_citta
                      [value] =&gt; Pesaro
                      [compare] =&gt; =
                  )
  
              [relation] =&gt; AND
          )
  
      [ignore_sticky_posts] =&gt; 
      [suppress_filters] =&gt; 
      [cache_results] =&gt; 1
      [update_post_term_cache] =&gt; 1
      [update_post_meta_cache] =&gt; 1
      [nopaging] =&gt; 
      [comments_per_page] =&gt; 50
      [no_found_rows] =&gt; 
      [order] =&gt; DESC
  )
  </code></pre>