我正在尝试建立一个音乐曲目搜索页面,让访问者:
选择帖子类型选择流派选择心情指定每分钟节拍(bpm)范围wp_dropdown_categories()
正在为其风格和情绪工作(即,我不需要修改pre_get_posts
), 但我不能将帖子限制在特定“bpm”范围内。
我在下面编写的代码要么在“bpm\\u start”和“bpm\\u finish”中分别选择20和180这样的特定范围时,根本不返回任何结果,要么在这样的搜索中得到奇怪的结果:
http://www.domain.com/?s=&post_type=ibm_tracks&ibm_genres=ambient-music&ibm_moods=0&bpm_start=20&bpm_finish=180
这应该会返回一堆
ibm_tracks
来自
ambient-music
类型和内部
bpm
自定义字段介于10和180之间,但由于某种原因它没有返回帖子。。。作为补充说明,上面的示例中没有指定语气,但选择一种语气不会影响结果,无论哪种方式都不会返回帖子。
有人能看出我做错了什么吗?
谢谢
Osu
Form
<?php
function osu_list_terms_dd($taxonomy_type, $taxonomy, $showalltext) {
$args = array(
\'taxonomy\' => $taxonomy_type,
\'hide_empty\' => true,
\'name\' => $taxonomy_type,
\'value_field\' => \'slug\',
\'show_option_all\' => $showalltext,
\'echo\' => 0,
//\'show_count\' => 1, // Useful for checking how many tracks are in a taxonomy term
\'id\' => \'taxonomy-filter__select-\' . $taxonomy,
\'orderby\' => \'name\',
\'order\' => \'ASC\'
);
return wp_dropdown_categories($args);
}
?>
<form role="search" method="get" class="search-form" action="<?php echo esc_url( home_url( \'/\' ) ); ?>">
<p>Search keywords:</p>
<input type="search" value="<?php echo get_search_query(); ?>" name="s" />
<p>Choose post type</p>
<select name="post_type">
<option value="ibm_tracks">TRACKS</option>
<option value="ibm_logos">LOGOS</option>
</select>
<p>Choose genre</p>
<?php echo osu_list_terms_dd(\'genres\', \'ibm_genres\', \'- choose a genre -\'); ?>
<p>Choose Mood</p>
<?php echo osu_list_terms_dd(\'moods\', \'ibm_moods\', \'- choose a mood -\'); ?>
<p><em>BPM start</em></p>
<select name="bpm_start">
<option value="0">0</option>
<option value="10">10</option>
<option value="20">20</option>
... etc...
<option value="220">220</option>
<option value="230">230</option>
<option value="240">240</option>
</select>
<p><em>BPM end</em></p>
<select name="bpm_finish">
<option value="0">0</option>
<option value="10">10</option>
<option value="20">20</option>
... etc...
<option value="220">220</option>
<option value="230">230</option>
<option value="240">240</option>
</select>
</form>
functions.php
// ADD CUSTOM QUERY VAR\'S SO BPM WORKS BELOW
function osu_add_query_vars_filter( $vars ){
$vars[] = "bpm_start";
$vars[] = "bpm_finish";
return $vars;
}
add_filter( \'query_vars\', \'osu_add_query_vars_filter\' );
// MODIFY THE MAIN QUERY OBJECT FOR TRACKS AND LOGOS CUSTOM POST TYPES
function osu_search_filter( $query ) {
// POST ARCHIVE PAGE : Only for IBM tracks and Logos on search page
if ( !is_admin() && isset($query->query_vars[\'post_type\']) && is_search() ) {
// Set vars
$post_type = $_GET[\'post_type\'];
$ibm_tracks = \'ibm_tracks\';
$ibm_logos = \'ibm_logos\';
if(isset($post_type)) {
// Only retrieve ibm_tracks
// ------------------------------------------------------------------
if($post_type == $ibm_tracks) {
// Set post type to ibm_tracks
$query->query_vars[\'post_type\'] = $ibm_tracks;
// Allow url to alter the query
$bpm_start = $_GET[\'bpm_start\'];
$bpm_finish = $_GET[\'bpm_finish\'];
// Filter posts by BPM range
if(isset($bpm_start) && isset($bpm_finish)) {
// Alter query with meta_query for BPM
$meta_query = array(
array(
\'key\' => \'bpm\',
\'value\' => array($bpm_start, $bpm_finish),
\'type\' => \'NUMERICAL\',
\'compare\' => \'BETWEEN\'
)
);
$query->set( \'meta_query\', $meta_query );
}
} // end if($post_type == ibm_tracks)
// Only retrieve ibm_logos
// ------------------------------------------------------------------
if($post_type == $ibm_logos) {
// Set post type to ibm_logos
$query->query_vars[\'post_type\'] = $ibm_logos;
}
} // end if(isset($post_type))
} // end if ( !is_admin() etc. )
return $query;
}
// Hook my above function to the pre_get_posts action
add_action( \'pre_get_posts\', \'osu_search_filter\' );