Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy is WP_Query's meta_query not filtering results?
    text
    copied!<p>I am working on a wordpress site that utilizes WP_Query to find songs based on their category, tags, and other things in a tax_query, but it also groups in a meta_query which (should) find songs based on bpm and other metadata.</p> <p>In the Media tab on Wordpress, admin can add songs, and attach them to a Product, listing track information such as composer, BPM, etc.</p> <p>All of the tax_query things (category, tags, moods, etc.) pull back songs properly. However, if I put in a BPM of 90--every track in the database is returned.</p> <p>What's going on with this? Here's the entire search function:</p> <pre><code>&lt;?php /* Template Name: Advanced Search Results */ get_header(); if( isset( $_POST['submit-track-search'] ) ) { // Build our Query $track_q = array( 's' =&gt; $_POST['track-search-term'], 'post_type' =&gt; 'product', 'post_status' =&gt; 'publish', ); $track_q['tax_query'] = array(); $track_q['meta_query'] = array(); $search_tax_fields = array(); $search_meta_fields = array(); if( isset( $_POST['track-catalog'] ) and $_POST['track-catalog'] != "-1" ) { $track_q['tax_query'][] = array( 'taxonomy' =&gt; 'product_catalog', 'field' =&gt; 'slug', 'terms' =&gt; $_POST['track-catalog'] ); $search_tax_fields[] = $_POST['track-catalog']; } if( isset( $_POST['track-tag'] ) and $_POST['track-tag'] != "-1" ) { $track_q['tax_query'][] = array( 'taxonomy' =&gt; 'product_tag', 'field' =&gt; 'slug', 'terms' =&gt; $_POST['track-tag'] ); $search_tax_fields[] = $_POST['track-tag']; } if( isset( $_POST['track-mood'] ) and $_POST['track-mood'] != "-1" ) { $track_q['tax_query'][] = array( 'taxonomy' =&gt; 'product_mood', 'field' =&gt; 'slug', 'terms' =&gt; $_POST['track-mood'] ); $search_tax_fields[] = $_POST['track-mood']; } if( isset( $_POST['track-style'] ) and $_POST['track-style'] != "-1" ) { $track_q['tax_query'][] = array( 'taxonomy' =&gt; 'product_style', 'field' =&gt; 'slug', 'terms' =&gt; $_POST['track-style'] ); $search_tax_fields[] = $_POST['track-style']; } if( isset( $_POST['track-composer'] ) and !empty( $_POST['track-composer'] ) ) { $search_meta_fields[] = $_POST['track-composer']; $track_q['meta_query'][] = array( 'meta_key' =&gt; '_track_composer', 'meta_value' =&gt; $_POST['track-composer'], 'compare' =&gt; 'LIKE' ); } if( isset( $_POST['track-publisher'] ) and !empty( $_POST['track-publisher'] ) ) { $search_meta_fields[] = $_POST['track-publisher']; $track_q['meta_query'][] = array( 'meta_key' =&gt; '_track_publisher', 'meta_value' =&gt; $_POST['track-publisher'], 'compare' =&gt; 'LIKE' ); } if( isset( $_POST['track-bpm'] ) and !empty( $_POST['track-bpm'] ) ) { $search_meta_fields[] = $_POST['track-bpm']; $track_q['meta_query'][] = array( 'meta_key' =&gt; '_track_bpm', 'meta_value' =&gt; $_POST['track-bpm'], 'compare' =&gt; '=' ); } if( isset( $_POST['track-temp'] ) and !empty( $_POST['track-temp'] ) ) { $search_meta_fields[] = $_POST['track-temp']; $track_q['meta_query'][] = array( 'meta_key' =&gt; '_track_temp', 'meta_value' =&gt; $_POST['track-temp'], 'compare' =&gt; 'LIKE' ); } if( count( $search_tax_fields ) &gt;= 2 ) { $track_q['tax_query']['relation'] = "AND"; } if( count( $search_meta_fields ) &gt;= 2 ) { $track_q['meta_query']['relation'] = "AND"; } $tracks = new WP_Query( $track_q ); } ?&gt; &lt;div id="content" class="grid_24"&gt; &lt;form id="advance-search" name="advance-search" method="post" action="http://premiumstockmusic.com/search-results"&gt; &lt;table width="100%" border="0" cellspacing="0" cellpadding="0" class="table" id="advanced-search-form"&gt; &lt;tr&gt; &lt;th width="26%" scope="row"&gt;Search For&lt;/tf&gt; &lt;td width="19%"&gt;&lt;input type="text" name="track-search-term" id="track-search-term" value="&lt;?php echo isset( $_POST['track-search-term'] ) ? $_POST['track-search-term'] : ''; ?&gt;" /&gt;&lt;/td&gt; &lt;td width="13%"&gt;&amp;nbsp;&lt;/td&gt; &lt;td width="12%"&gt;Catalog&lt;/td&gt; &lt;td width="30%"&gt; &lt;select name="track-catalog" id="track-catalog"&gt; &lt;option value="-1"&gt;Please Select One&lt;/option&gt; &lt;?php $catalog_terms = get_terms( 'product_catalog' ); if( isset( $_POST['track-catalog'] ) ) { $selection = ' selected="selected"'; $selected_term = $_POST['track-catalog']; } foreach( $catalog_terms as $catalog ) { if( $catalog-&gt;slug != $selected_term ) { echo '&lt;option name="option'. $catalog-&gt;slug .'" value="'. $catalog-&gt;slug .'"&gt;'. $catalog-&gt;name .'&lt;/option&gt;'; } else { echo '&lt;option name="option'. $catalog-&gt;slug .'" value="'. $catalog-&gt;slug .'"'. $selection .'&gt;'. $catalog-&gt;name .'&lt;/option&gt;'; } } ?&gt; &lt;/select&gt; &lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;th scope="row"&gt;Moods&lt;/th&gt; &lt;td&gt; &lt;select name="track-mood" id="track-mood"&gt; &lt;option value="-1"&gt;Please Select One&lt;/option&gt; &lt;?php $mood_terms = get_terms( 'product_mood' ); if( isset( $_POST['track-mood'] ) ) { $selection = ' selected="selected"'; $selected_term = $_POST['track-mood']; } foreach( $mood_terms as $mood ) { if( $mood-&gt;slug != $selected_term ) { echo '&lt;option name="option'. $mood-&gt;slug .'" value="'. $mood-&gt;slug .'"&gt;'. $mood-&gt;name .'&lt;/option&gt;'; } else { echo '&lt;option name="option'. $mood-&gt;slug .'" value="'. $mood-&gt;slug .'"'. $selection .'&gt;'. $mood-&gt;name .'&lt;/option&gt;'; } } ?&gt; &lt;/select&gt; &lt;/td&gt; &lt;td&gt;&amp;nbsp;&lt;/td&gt; &lt;td&gt;Music Styles&lt;/td&gt; &lt;td&gt; &lt;select name="track-style" id="track-style"&gt; &lt;option value="-1"&gt;Please Select One&lt;/option&gt; &lt;?php $style_terms = get_terms( 'product_style' ); if( isset( $_POST['track-style'] ) ) { $selection = ' selected="selected"'; $selected_term = $_POST['track-style']; } foreach( $style_terms as $style ) { if( $style-&gt;slug != $selected_term ) { echo '&lt;option name="option'. $style-&gt;slug .'" value="'. $style-&gt;slug .'"&gt;'. $style-&gt;name .'&lt;/option&gt;'; } else { echo '&lt;option name="option'. $style-&gt;slug .'" value="'. $style-&gt;slug .'"'. $selection .'&gt;'. $style-&gt;name .'&lt;/option&gt;'; } } ?&gt; &lt;/select&gt; &lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;th scope="row"&gt;Tagged With&lt;/th&gt; &lt;td&gt; &lt;select name="track-tag" id="track-tag"&gt; &lt;option value="-1"&gt;Please Select One&lt;/option&gt; &lt;?php $catalog_tags = get_terms( 'product_tag' ); if( isset( $_POST['track-tag'] ) ) { $selection = ' selected="selected"'; $selected_tag = $_POST['track-tag']; } foreach( $catalog_tags as $tag ) { if( $tag-&gt;slug != $selected_tag ) { echo '&lt;option name="option'. $tag-&gt;slug .'" value="'. $tag-&gt;slug .'"&gt;'. $tag-&gt;name .'&lt;/option&gt;'; } else { echo '&lt;option name="option'. $tag-&gt;slug .'" value="'. $tag-&gt;slug .'"'. $selection .'&gt;'. $tag-&gt;name .'&lt;/option&gt;'; } } ?&gt; &lt;/select&gt; &lt;/td&gt; &lt;td&gt;&amp;nbsp;&lt;/td&gt; &lt;td&gt;Tempo&lt;/td&gt; &lt;td&gt;&lt;input type="text" name="track-temp" id="track-temp" value="&lt;?php echo isset( $_POST['track-temp'] ) ? $_POST['track-temp'] : ''; ?&gt;" /&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;th scope="row"&gt;BPM&lt;/th&gt; &lt;td&gt;&lt;input type="text" name="track-bpm" id="track-bpm" value="&lt;?php echo isset( $_POST['track-bpm'] ) ? $_POST['track-bpm'] : ''; ?&gt;" /&gt;&lt;/td&gt; &lt;td&gt;&amp;nbsp;&lt;/td&gt; &lt;td&gt;Instruments&lt;/td&gt; &lt;td&gt; &lt;select name="track-instrument" id="track-instrument"&gt; &lt;option value="-1"&gt;Please Select One&lt;/option&gt; &lt;?php $instrument_tags = get_terms( 'product_instrument' ); if( isset( $_POST['track-instrument'] ) ) { $selection = ' selected="selected"'; $selected_tag = $_POST['track-instrument']; } foreach( $instrument_tags as $instrument ) { if( $instrument-&gt;slug != $selected_tag ) { echo '&lt;option name="option'. $instrument-&gt;slug .'" value="'. $instrument-&gt;slug .'"&gt;'. $instrument-&gt;name .'&lt;/option&gt;'; } else { echo '&lt;option name="option'. $instrument-&gt;slug .'" value="'. $instrument-&gt;slug .'"'. $selection .'&gt;'. $instrument-&gt;name .'&lt;/option&gt;'; } } ?&gt; &lt;/select&gt; &lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;th scope="row"&gt;&lt;input type="submit" name="submit-track-search" id="submit-track-search" value="Search Our Tracks" /&gt;&lt;/th&gt; &lt;td&gt;&amp;nbsp;&lt;/td&gt; &lt;td&gt;&amp;nbsp;&lt;/td&gt; &lt;td&gt;&amp;nbsp;&lt;/td&gt; &lt;td&gt;&amp;nbsp;&lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; &lt;/form&gt; &lt;?php if ( $tracks-&gt;have_posts() ): ?&gt; &lt;div id="post-&lt;?php the_ID(); ?&gt;" &lt;?php post_class('product_search_result'); ?&gt;&gt; &lt;h1&gt;Search Results&lt;/h1&gt; &lt;div class="catalog-items"&gt; &lt;?php while( $tracks-&gt;have_posts() ): $tracks-&gt;the_post(); ?&gt; &lt;div class="catalog-item full-length medium-length"&gt; &lt;div class="toggle"&gt;&lt;a class="trigger" href="#"&gt;&lt;span&gt; +&lt;/span&gt;&lt;?php the_title(); ?&gt;&lt;/a&gt; &lt;div class="box" style="display: none;"&gt; &lt;table width="100%" border="0" cellspacing="0" cellpadding="0" class="table table-border table-hover"&gt; &lt;tr&gt; &lt;th scope="row"&gt;Buy Now:&lt;/th&gt; &lt;td&gt;&lt;a href="&lt;?php global $product; echo $product-&gt;add_to_cart_url( get_the_ID() ); ?&gt;" class="add-to-cart-button"&gt;&lt;span class="middle"&gt;Add To Cart&lt;/span&gt;&lt;/a&gt;&lt;/td&gt; &lt;/tr&gt; &lt;th scope="row"&gt;Description:&lt;/th&gt; &lt;td&gt;&lt;?php the_excerpt(); ?&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;th scope="row"&gt;Catalog:&lt;/th&gt; &lt;td&gt;&lt;?php the_terms( get_the_ID(), 'product_catalog', '', ', ', '' ); ?&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;th scope="row"&gt;Moods:&lt;/th&gt; &lt;td&gt;&lt;?php the_terms( get_the_ID(), 'product_mood', '', ', ', '' ); ?&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;tr&gt; &lt;th scope="row"&gt;Music Style:&lt;/th&gt; &lt;td&gt;&lt;?php the_terms( get_the_ID(), 'product_style', '', ', ', '' ); ?&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;tr&gt; &lt;th scope="row"&gt;Instruments:&lt;/th&gt; &lt;td&gt;&lt;?php the_terms( get_the_ID(), 'product_instrument', '', ', ', '' ); ?&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;th scope="row"&gt;Composer:&lt;/th&gt; &lt;td&gt;&lt;?php echo get_post_meta( get_the_ID(), '_track_composer', true ); ?&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;th scope="row"&gt;Publisher:&lt;/th&gt; &lt;td&gt;&lt;?php echo get_post_meta( get_the_ID(), '_track_publisher', true ); ?&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;th scope="row"&gt;Keywords:&lt;/th&gt; &lt;td&gt;&lt;?php echo $product-&gt;get_tags( ', ', '&lt;span class="tagged_as"&gt;', '&lt;/span&gt;' ); ?&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;th scope="row"&gt;Tempo:&lt;/th&gt; &lt;td&gt;&lt;?php echo get_post_meta( get_the_ID(), '_track_temp', true ); ?&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;th scope="row"&gt;BPM:&lt;/th&gt; &lt;td&gt;&lt;?php echo get_post_meta( get_the_ID(), '_track_bpm', true ); ?&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;th scope="row"&gt;Length:&lt;/th&gt; &lt;td&gt;&lt;?php echo get_post_meta( get_the_ID(), '_track_length', true ); ?&gt;&lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; &lt;/div&gt; &lt;/div&gt; &lt;?php echo do_shortcode( '[audio file="'.get_post_meta( get_the_ID(), '_track_preview_file', true ).'"]' ); ?&gt; &lt;/div&gt;&lt;!-- .catalog-item --&gt; &lt;?php endwhile; else: ?&gt; &lt;p&gt;No results found that match those settings.&lt;/p&gt; &lt;?php endif; ?&gt; &lt;/div&gt;&lt;!-- .catalog-items --&gt; &lt;/div&gt;&lt;!--#post-# .post--&gt; &lt;/div&gt;&lt;!--#content--&gt; &lt;?php get_footer(); ?&gt; </code></pre> <p><strong>EDIT:</strong></p> <p>The result of var_dump($tracks); (without the results, since all items are being returned for some reason) is:</p> <pre><code>object(WP_Query)#714 (45) { ["query_vars"]=&gt; array(61) { ["s"]=&gt; string(0) "" ["post_type"]=&gt; string(7) "product" ["post_status"]=&gt; string(7) "publish" ["tax_query"]=&gt; array(0) { } ["meta_query"]=&gt; array(1) { [0]=&gt; array(3) { ["meta_key"]=&gt; string(10) "_track_bpm" ["meta_value"]=&gt; string(2) "90" ["compare"]=&gt; string(1) "=" } } ["error"]=&gt; string(0) "" ["m"]=&gt; int(0) ["p"]=&gt; int(0) ["post_parent"]=&gt; string(0) "" ["subpost"]=&gt; string(0) "" ["subpost_id"]=&gt; string(0) "" ["attachment"]=&gt; string(0) "" ["attachment_id"]=&gt; int(0) ["name"]=&gt; string(0) "" ["static"]=&gt; string(0) "" ["pagename"]=&gt; string(0) "" ["page_id"]=&gt; int(0) ["second"]=&gt; string(0) "" ["minute"]=&gt; string(0) "" ["hour"]=&gt; string(0) "" ["day"]=&gt; int(0) ["monthnum"]=&gt; int(0) ["year"]=&gt; int(0) ["w"]=&gt; int(0) ["category_name"]=&gt; string(0) "" ["tag"]=&gt; string(0) "" ["cat"]=&gt; string(0) "" ["tag_id"]=&gt; string(0) "" ["author_name"]=&gt; string(0) "" ["feed"]=&gt; string(0) "" ["tb"]=&gt; string(0) "" ["paged"]=&gt; int(0) ["comments_popup"]=&gt; string(0) "" ["meta_key"]=&gt; string(0) "" ["meta_value"]=&gt; string(0) "" ["preview"]=&gt; string(0) "" ["sentence"]=&gt; string(0) "" ["fields"]=&gt; string(0) "" ["menu_order"]=&gt; string(0) "" ["category__in"]=&gt; array(0) { } ["category__not_in"]=&gt; array(0) { } ["category__and"]=&gt; array(0) { } ["post__in"]=&gt; array(0) { } ["post__not_in"]=&gt; array(0) { } ["tag__in"]=&gt; array(0) { } ["tag__not_in"]=&gt; array(0) { } ["tag__and"]=&gt; array(0) { } ["tag_slug__in"]=&gt; array(0) { } ["tag_slug__and"]=&gt; array(0) { } ["post_parent__in"]=&gt; array(0) { } ["post_parent__not_in"]=&gt; array(0) { } ["ignore_sticky_posts"]=&gt; bool(false) ["suppress_filters"]=&gt; bool(false) ["cache_results"]=&gt; bool(true) ["update_post_term_cache"]=&gt; bool(true) ["update_post_meta_cache"]=&gt; bool(true) ["posts_per_page"]=&gt; int(50) ["nopaging"]=&gt; bool(false) ["comments_per_page"]=&gt; string(2) "50" ["no_found_rows"]=&gt; bool(false) ["order"]=&gt; string(4) "DESC" } ["tax_query"]=&gt; object(WP_Tax_Query)#727 (2) { ["queries"]=&gt; array(0) { } ["relation"]=&gt; string(3) "AND" } ["meta_query"]=&gt; object(WP_Meta_Query)#728 (2) { ["queries"]=&gt; array(1) { [0]=&gt; array(3) { ["meta_key"]=&gt; string(10) "_track_bpm" ["meta_value"]=&gt; string(2) "90" ["compare"]=&gt; string(1) "=" } } ["relation"]=&gt; string(3) "AND" } ["post_count"]=&gt; int(50) ["current_post"]=&gt; int(-1) ["in_the_loop"]=&gt; bool(false) ["comment_count"]=&gt; int(0) ["current_comment"]=&gt; int(-1) ["found_posts"]=&gt; string(3) "109" ["max_num_pages"]=&gt; float(3) ["max_num_comment_pages"]=&gt; int(0) ["is_single"]=&gt; bool(false) ["is_preview"]=&gt; bool(false) ["is_page"]=&gt; bool(false) ["is_archive"]=&gt; bool(true) ["is_date"]=&gt; bool(false) ["is_year"]=&gt; bool(false) ["is_month"]=&gt; bool(false) ["is_day"]=&gt; bool(false) ["is_time"]=&gt; bool(false) ["is_author"]=&gt; bool(false) ["is_category"]=&gt; bool(false) ["is_tag"]=&gt; bool(false) ["is_tax"]=&gt; bool(false) ["is_search"]=&gt; bool(false) ["is_feed"]=&gt; bool(false) ["is_comment_feed"]=&gt; bool(false) ["is_trackback"]=&gt; bool(false) ["is_home"]=&gt; bool(false) ["is_404"]=&gt; bool(false) ["is_comments_popup"]=&gt; bool(false) ["is_paged"]=&gt; bool(false) ["is_admin"]=&gt; bool(false) ["is_attachment"]=&gt; bool(false) ["is_singular"]=&gt; bool(false) ["is_robots"]=&gt; bool(false) ["is_posts_page"]=&gt; bool(false) ["is_post_type_archive"]=&gt; bool(true) ["query_vars_hash"]=&gt; string(32) "ac0d4d0d12ae36d5be9a88790932d407" ["query_vars_changed"]=&gt; bool(false) ["thumbnails_cached"]=&gt; bool(false) ["query"]=&gt; array(5) { ["s"]=&gt; string(0) "" ["post_type"]=&gt; string(7) "product" ["post_status"]=&gt; string(7) "publish" ["tax_query"]=&gt; array(0) { } ["meta_query"]=&gt; array(1) { [0]=&gt; array(3) { ["meta_key"]=&gt; string(10) "_track_bpm" ["meta_value"]=&gt; string(2) "90" ["compare"]=&gt; string(1) "=" } } } ["request"]=&gt; string(205) "SELECT SQL_CALC_FOUND_ROWS wp_posts.ID FROM wp_posts WHERE 1=1 AND wp_posts.post_type = 'product' AND (wp_posts.post_status = 'publish') GROUP BY wp_posts.ID ORDER BY wp_posts.post_date DESC LIMIT 0, 50" </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload