为什么WP_Query的meta_query不会过滤结果?

时间:2013-11-03 03:26:38

标签: php wordpress

我正在使用一个wordpress网站,该网站利用WP_Query根据tax_query中的类别,标签和其他内容查找歌曲,但它也会在meta_query中进行分组,该元数据应该根据bpm和其他元数据查找歌曲。

在Wordpress的媒体标签中,管理员可以添加歌曲,并将其附加到产品,列出曲目信息,如作曲家,BPM等。

所有tax_query事物(类别,标签,情绪等)都适当地拉回歌曲。但是,如果我输入90的BPM - 将返回数据库中的每个轨道。

这是怎么回事?这是整个搜索功能:

<?php
/* Template Name: Advanced Search Results */
get_header();

if( isset( $_POST['submit-track-search'] ) ) {
// Build our Query
$track_q = array(
  's' => $_POST['track-search-term'],
  'post_type'   => 'product',
  'post_status' => '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'    => 'product_catalog',
    'field'       => 'slug',
    'terms'       => $_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'    => 'product_tag',
    'field'       => 'slug',
    'terms'       => $_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'    => 'product_mood',
    'field'       => 'slug',
    'terms'       => $_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'    => 'product_style',
    'field'       => 'slug',
    'terms'       => $_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'    => '_track_composer',
      'meta_value'  => $_POST['track-composer'],
      'compare'     => 'LIKE'
  );
}
if( isset( $_POST['track-publisher'] ) and !empty( $_POST['track-publisher'] ) ) {
  $search_meta_fields[] = $_POST['track-publisher'];

  $track_q['meta_query'][] =  array(
      'meta_key'    => '_track_publisher',
      'meta_value'  => $_POST['track-publisher'],
      'compare'     => 'LIKE'
  );
}
if( isset( $_POST['track-bpm'] ) and !empty( $_POST['track-bpm'] ) ) {
  $search_meta_fields[] = $_POST['track-bpm'];

  $track_q['meta_query'][] =  array(
      'meta_key'    => '_track_bpm',
      'meta_value'  => $_POST['track-bpm'],
      'compare'     => '='
  );
}
if( isset( $_POST['track-temp'] ) and !empty( $_POST['track-temp'] ) ) {
  $search_meta_fields[] = $_POST['track-temp'];

  $track_q['meta_query'][] =  array(
      'meta_key'    => '_track_temp',
      'meta_value'  => $_POST['track-temp'],
      'compare'     => 'LIKE'
  );
}

if( count( $search_tax_fields ) >= 2 ) {
  $track_q['tax_query']['relation'] = "AND";
}

if( count( $search_meta_fields ) >= 2 ) {
  $track_q['meta_query']['relation'] = "AND";
}

$tracks = new WP_Query( $track_q );
}
?>

<div id="content" class="grid_24">
<form id="advance-search" name="advance-search" method="post" action="http://premiumstockmusic.com/search-results"> 
<table width="100%" border="0" cellspacing="0" cellpadding="0" class="table" id="advanced-search-form">
  <tr>
    <th width="26%" scope="row">Search For</tf>
    <td width="19%"><input type="text" name="track-search-term" id="track-search-term" value="<?php echo isset( $_POST['track-search-term'] ) ? $_POST['track-search-term'] : ''; ?>" /></td>
    <td width="13%">&nbsp;</td>
    <td width="12%">Catalog</td>
    <td width="30%">
    <select name="track-catalog" id="track-catalog">
      <option value="-1">Please Select One</option>
      <?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->slug != $selected_term ) {
          echo '<option name="option'. $catalog->slug .'" value="'. $catalog->slug .'">'. $catalog->name .'</option>';
        } else {
          echo '<option name="option'. $catalog->slug .'" value="'. $catalog->slug .'"'. $selection .'>'. $catalog->name .'</option>';
        }
      }
      ?>
    </select>
    </td>
  </tr>
  <tr>
    <th scope="row">Moods</th>
    <td>
    <select name="track-mood" id="track-mood">
      <option value="-1">Please Select One</option>
      <?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->slug != $selected_term ) {
          echo '<option name="option'. $mood->slug .'" value="'. $mood->slug .'">'. $mood->name .'</option>';
        } else {
          echo '<option name="option'. $mood->slug .'" value="'. $mood->slug .'"'. $selection .'>'. $mood->name .'</option>';
        }
      }
      ?>
    </select>
    </td>
    <td>&nbsp;</td>
    <td>Music Styles</td>
    <td>
    <select name="track-style" id="track-style">
      <option value="-1">Please Select One</option>
      <?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->slug != $selected_term ) {
          echo '<option name="option'. $style->slug .'" value="'. $style->slug .'">'. $style->name .'</option>';
        } else {
          echo '<option name="option'. $style->slug .'" value="'. $style->slug .'"'. $selection .'>'. $style->name .'</option>';
        }
      }
      ?>
    </select>
    </td>
  </tr>
  <tr>
    <th scope="row">Tagged With</th>
    <td>
    <select name="track-tag" id="track-tag">
      <option value="-1">Please Select One</option>
      <?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->slug != $selected_tag ) {
          echo '<option name="option'. $tag->slug .'" value="'. $tag->slug .'">'. $tag->name .'</option>';
        } else {
          echo '<option name="option'. $tag->slug .'" value="'. $tag->slug .'"'. $selection .'>'. $tag->name .'</option>';
        }
      }
      ?>
    </select>
    </td>
    <td>&nbsp;</td>
    <td>Tempo</td>
    <td><input type="text" name="track-temp" id="track-temp" value="<?php echo isset( $_POST['track-temp'] ) ? $_POST['track-temp'] : ''; ?>" /></td>
  </tr>
  <tr>
    <th scope="row">BPM</th>
    <td><input type="text" name="track-bpm" id="track-bpm" value="<?php echo isset( $_POST['track-bpm'] ) ? $_POST['track-bpm'] : ''; ?>" /></td>
    <td>&nbsp;</td>
    <td>Instruments</td>
    <td>
    <select name="track-instrument" id="track-instrument">
      <option value="-1">Please Select One</option>
      <?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->slug != $selected_tag ) {
          echo '<option name="option'. $instrument->slug .'" value="'. $instrument->slug .'">'. $instrument->name .'</option>';
        } else {
          echo '<option name="option'. $instrument->slug .'" value="'. $instrument->slug .'"'. $selection .'>'. $instrument->name .'</option>';
        }
      }
      ?>
    </select>
    </td>
  </tr>
  <tr>
    <th scope="row"><input type="submit" name="submit-track-search" id="submit-track-search" value="Search Our Tracks" /></th>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
</table>
</form>
  <?php if ( $tracks->have_posts() ):  ?>
    <div id="post-<?php the_ID(); ?>" <?php post_class('product_search_result'); ?>>
    <h1>Search Results</h1>
      <div class="catalog-items">
  <?php while( $tracks->have_posts() ): $tracks->the_post(); ?>
        <div class="catalog-item full-length medium-length">
        <div class="toggle"><a class="trigger" href="#"><span> +</span><?php the_title(); ?></a>
        <div class="box" style="display: none;">
        <table width="100%" border="0" cellspacing="0" cellpadding="0" class="table table-border table-hover">
          <tr>
            <th scope="row">Buy Now:</th>
            <td><a href="<?php global $product; echo $product->add_to_cart_url( get_the_ID() ); ?>" class="add-to-cart-button"><span class="middle">Add To Cart</span></a></td>
          </tr>
            <th scope="row">Description:</th>
            <td><?php the_excerpt(); ?></td>
          </tr>
          <tr>
            <th scope="row">Catalog:</th>
            <td><?php the_terms( get_the_ID(), 'product_catalog', '', ', ', '' ); ?></td>
          </tr>
          <tr>
            <th scope="row">Moods:</th>
            <td><?php the_terms( get_the_ID(), 'product_mood', '', ', ', '' ); ?></td>
          </tr>
          <tr>
          <tr>
            <th scope="row">Music Style:</th>
            <td><?php the_terms( get_the_ID(), 'product_style', '', ', ', '' ); ?></td>
          </tr>
          <tr>
          <tr>
            <th scope="row">Instruments:</th>
            <td><?php the_terms( get_the_ID(), 'product_instrument', '', ', ', '' ); ?></td>
          </tr>
          <tr>
            <th scope="row">Composer:</th>
            <td><?php echo get_post_meta( get_the_ID(), '_track_composer', true ); ?></td>
          </tr>
          <tr>
            <th scope="row">Publisher:</th>
            <td><?php echo get_post_meta( get_the_ID(), '_track_publisher', true ); ?></td>
          </tr>
          <tr>
            <th scope="row">Keywords:</th>
            <td><?php echo $product->get_tags( ', ', '<span class="tagged_as">', '</span>' ); ?></td>
          </tr>
          <tr>
            <th scope="row">Tempo:</th>
            <td><?php echo get_post_meta( get_the_ID(), '_track_temp', true ); ?></td>
          </tr>
          <tr>
            <th scope="row">BPM:</th>
            <td><?php echo get_post_meta( get_the_ID(), '_track_bpm', true ); ?></td>
          </tr>
          <tr>
            <th scope="row">Length:</th>
            <td><?php echo get_post_meta( get_the_ID(), '_track_length', true ); ?></td>
          </tr>
        </table>
        </div>
        </div>

        <?php echo do_shortcode( '[audio file="'.get_post_meta( get_the_ID(), '_track_preview_file', true ).'"]' ); ?>


        </div><!-- .catalog-item -->
  <?php endwhile; else: ?>
  <p>No results found that match those settings.</p>
  <?php endif; ?>
      </div><!-- .catalog-items -->
    </div><!--#post-# .post-->
</div><!--#content-->
<?php get_footer(); ?>

修改

var_dump($ tracks)的结果; (没有结果,因为所有项目由于某种原因被退回)是:

object(WP_Query)#714 (45) { ["query_vars"]=> 
  array(61) { ["s"]=> string(0) "" 
              ["post_type"]=> string(7) "product" 
              ["post_status"]=> string(7) "publish" 
              ["tax_query"]=> array(0) { } 
              ["meta_query"]=> array(1) { 
                [0]=> array(3) { 
                  ["meta_key"]=> string(10) "_track_bpm" 
                  ["meta_value"]=> string(2) "90" 
                  ["compare"]=> string(1) "=" 
                } 
              } 
              ["error"]=> string(0) "" 
              ["m"]=> int(0) ["p"]=> int(0) 
              ["post_parent"]=> string(0) "" 
              ["subpost"]=> string(0) "" 
              ["subpost_id"]=> string(0) "" 
              ["attachment"]=> string(0) "" 
              ["attachment_id"]=> int(0) 
              ["name"]=> string(0) "" 
              ["static"]=> string(0) "" 
              ["pagename"]=> string(0) "" 
              ["page_id"]=> int(0) 
              ["second"]=> string(0) "" 
              ["minute"]=> string(0) "" 
              ["hour"]=> string(0) "" 
              ["day"]=> int(0) 
              ["monthnum"]=> int(0) 
              ["year"]=> int(0) 
              ["w"]=> int(0) 
              ["category_name"]=> string(0) "" 
              ["tag"]=> string(0) "" 
              ["cat"]=> string(0) "" 
              ["tag_id"]=> string(0) "" 
              ["author_name"]=> string(0) "" 
              ["feed"]=> string(0) "" 
              ["tb"]=> string(0) "" 
              ["paged"]=> int(0) 
              ["comments_popup"]=> string(0) "" 
              ["meta_key"]=> string(0) "" 
              ["meta_value"]=> string(0) "" 
              ["preview"]=> string(0) "" 
              ["sentence"]=> string(0) "" 
              ["fields"]=> string(0) "" 
              ["menu_order"]=> string(0) "" 
              ["category__in"]=> array(0) { } 
              ["category__not_in"]=> array(0) { } 
              ["category__and"]=> array(0) { } 
              ["post__in"]=> array(0) { } 
              ["post__not_in"]=> array(0) { } 
              ["tag__in"]=> array(0) { } 
              ["tag__not_in"]=> array(0) { } 
              ["tag__and"]=> array(0) { } 
              ["tag_slug__in"]=> array(0) { } 
              ["tag_slug__and"]=> array(0) { } 
              ["post_parent__in"]=> array(0) { } 
              ["post_parent__not_in"]=> array(0) { } 
              ["ignore_sticky_posts"]=> bool(false) 
              ["suppress_filters"]=> bool(false) 
              ["cache_results"]=> bool(true) 
              ["update_post_term_cache"]=> bool(true) 
              ["update_post_meta_cache"]=> bool(true) 
              ["posts_per_page"]=> int(50) 
              ["nopaging"]=> bool(false) 
              ["comments_per_page"]=> string(2) "50" 
              ["no_found_rows"]=> bool(false) 
              ["order"]=> string(4) "DESC" } 
              ["tax_query"]=> object(WP_Tax_Query)#727 (2) { 
                ["queries"]=> array(0) { } 
                ["relation"]=> string(3) "AND" 
              } 
              ["meta_query"]=> object(WP_Meta_Query)#728 (2) { 
                ["queries"]=> array(1) { 
                  [0]=> array(3) { 
                    ["meta_key"]=> string(10) "_track_bpm" 
                    ["meta_value"]=> string(2) "90" 
                    ["compare"]=> string(1) "=" 
                  } 
                } 
                ["relation"]=> string(3) "AND" 
              } 
              ["post_count"]=> int(50) 
              ["current_post"]=> int(-1) 
              ["in_the_loop"]=> bool(false) 
              ["comment_count"]=> int(0) 
              ["current_comment"]=> int(-1) 
              ["found_posts"]=> string(3) "109" 
              ["max_num_pages"]=> float(3) 
              ["max_num_comment_pages"]=> int(0) 
              ["is_single"]=> bool(false) 
              ["is_preview"]=> bool(false) 
              ["is_page"]=> bool(false) 
              ["is_archive"]=> bool(true) 
              ["is_date"]=> bool(false) 
              ["is_year"]=> bool(false) 
              ["is_month"]=> bool(false) 
              ["is_day"]=> bool(false) 
              ["is_time"]=> bool(false) 
              ["is_author"]=> bool(false) 
              ["is_category"]=> bool(false) 
              ["is_tag"]=> bool(false) 
              ["is_tax"]=> bool(false) 
              ["is_search"]=> bool(false) 
              ["is_feed"]=> bool(false) 
              ["is_comment_feed"]=> bool(false) 
              ["is_trackback"]=> bool(false) 
              ["is_home"]=> bool(false) 
              ["is_404"]=> bool(false) 
              ["is_comments_popup"]=> bool(false) 
              ["is_paged"]=> bool(false) 
              ["is_admin"]=> bool(false) 
              ["is_attachment"]=> bool(false) 
              ["is_singular"]=> bool(false) 
              ["is_robots"]=> bool(false) 
              ["is_posts_page"]=> bool(false) 
              ["is_post_type_archive"]=> bool(true) 
              ["query_vars_hash"]=> string(32) "ac0d4d0d12ae36d5be9a88790932d407" ["query_vars_changed"]=> bool(false) ["thumbnails_cached"]=> bool(false) 
              ["query"]=> array(5) { 
                ["s"]=> string(0) "" 
                ["post_type"]=> string(7) "product" 
                ["post_status"]=> string(7) "publish" 
                ["tax_query"]=> array(0) { } 
                ["meta_query"]=> array(1) { 
                  [0]=> array(3) { 
                    ["meta_key"]=> string(10) "_track_bpm" 
                    ["meta_value"]=> string(2) "90" 
                    ["compare"]=> string(1) "=" 
                  } 
                } 
              } 
              ["request"]=> 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"

1 个答案:

答案 0 :(得分:1)

我应该仔细研究一下我在评论中发布的例子。

if( isset( $_POST['track-bpm'] ) and !empty( $_POST['track-bpm'] ) ) {
  $search_meta_fields[] = $_POST['track-bpm'];

  $track_q['meta_query'][] =  array(
      'meta_key'    => '_track_bpm',
      'meta_value'  => $_POST['track-bpm'],
      'compare'     => '='
  );
}

您的阵列的键是错误的。它们应为keyvalue,而不是meta_keymeta_value。这应该有效:

if( isset( $_POST['track-bpm'] ) and !empty( $_POST['track-bpm'] ) ) {
  $search_meta_fields[] = $_POST['track-bpm'];

  $track_q['meta_query'][] =  array(
      'key'      => '_track_bpm',
      'value'    => $_POST['track-bpm'],
      'compare'  => '='
  );
}

同样适用于您的其他meta_query条件。