Php 为什么WP#u查询';s meta_查询未筛选结果?
我在一个wordpress网站上工作,该网站利用WP_查询在tax_查询中根据歌曲的类别、标签和其他内容查找歌曲,但它也在meta_查询中分组,meta_查询(应该)根据bpm和其他元数据查找歌曲 在Wordpress的“媒体”选项卡中,管理员可以添加歌曲,并将其附加到产品,列出曲目信息,如作曲家、BPM等 所有税务查询内容(类别、标签、情绪等)都会正确地收回歌曲。但是,如果我将BPM设置为90,则会返回数据库中的每个轨迹 这是怎么回事?以下是整个搜索功能:Php 为什么WP#u查询';s meta_查询未筛选结果?,php,wordpress,Php,Wordpress,我在一个wordpress网站上工作,该网站利用WP_查询在tax_查询中根据歌曲的类别、标签和其他内容查找歌曲,但它也在meta_查询中分组,meta_查询(应该)根据bpm和其他元数据查找歌曲 在Wordpress的“媒体”选项卡中,管理员可以添加歌曲,并将其附加到产品,列出曲目信息,如作曲家、BPM等 所有税务查询内容(类别、标签、情绪等)都会正确地收回歌曲。但是,如果我将BPM设置为90,则会返回数据库中的每个轨迹 这是怎么回事?以下是整个搜索功能: <?php /* Templa
<?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%"> </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> </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> </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> </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> </td>
<td> </td>
<td> </td>
<td> </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(); ?>
我应该仔细看看我在评论中发布的例子。你有
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-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\u查询
条件可能也是如此。我可能遗漏了一些内容,但我可以看到正在填充$search\u tax\u字段
和$search\u meta\u字段
,但我看不到它们正在查询中使用。输入是什么?”90'或'90--'?@SidGBF50输入是'90'@Hobo它们用于计数我想(我继承了这段代码)如果添加'type'=>'numeric'
之类的内容会发生什么?
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' => '='
);
}