Wordpress:如何按slug(类别名称)在特定类别内搜索

Wordpress:如何按slug(类别名称)在特定类别内搜索,wordpress,Wordpress,我如何创建一个代码,通过slug(类别名称)搜索,而不是通过ID。我如何为我的客户创建一个定制的解决方案,以便在特定类别(没有任何插件)内搜索 解释:我希望对搜索代码进行定制,使客户可以在特定类别内搜索,而无需编辑任何代码。谢谢 实际上,您应该检查作者在哪里对搜索功能自定义进行了非常详细的解释 您的案例中最有趣的部分之一是: // meta_query expects nested arrays even if you only have one query $sm_query = new WP

我如何创建一个代码,通过slug(类别名称)搜索,而不是通过ID。我如何为我的客户创建一个定制的解决方案,以便在特定类别(没有任何插件)内搜索


解释:我希望对搜索代码进行定制,使客户可以在特定类别内搜索,而无需编辑任何代码。谢谢

实际上,您应该检查作者在哪里对搜索功能自定义进行了非常详细的解释

您的案例中最有趣的部分之一是:

// meta_query expects nested arrays even if you only have one query
$sm_query = new WP_Query( array( 'post_type' => 'accommodation', 'posts_per_page' => '-1', 'meta_query' => array( array( 'key' => '_sm_accommodation_city' ) ) ) );

// The Loop
if ( $sm_query->have_posts() ) {
    $cities = array();
    while ( $sm_query->have_posts() ) {
        $sm_query->the_post();
        $city = get_post_meta( get_the_ID(), '_sm_accommodation_city', true );

        // populate an array of all occurrences (non duplicated)
        if( !in_array( $city, $cities ) ){
            $cities[] = $city;    
        }
    }
}
} else{
       echo 'No accommodations yet!';
       return;
}


/* Restore original Post Data */
wp_reset_postdata();

if( count($cities) == 0){
    return;
}

asort($cities);

$select_city = '<select name="city" style="width: 100%">';
$select_city .= '<option value="" selected="selected">' . __( 'Select city', 'smashing_plugin' ) . '</option>';
foreach ($cities as $city ) {
    $select_city .= '<option value="' . $city . '">' . $city . '</option>';
}
$select_city .= '</select>' . "\n";

reset($cities);
//即使只有一个查询,meta_查询也需要嵌套数组
$sm_query=new WP_query(数组('post_type'=>'住宿','posts_per_page'=>'-1','meta_query'=>数组('key'=>'sm_住宿城市'));
//环路
如果($sm\u query->have\u posts()){
$cities=array();
而($sm\u query->have\u posts()){
$sm_query->the_post();
$city=get_post_meta(get_ID(),'u sm_住宿_city',true);
//填充所有引用的数组(非重复)
if(!in_数组($city,$cities)){
$cities[]=$city;
}
}
}
}否则{
echo“还没有住宿!”;
返回;
}
/*恢复原始Post数据*/
wp_reset_postdata();
如果(计数($城市)=0){
返回;
}
asort(城市);
$select_city='';
$select_city.=''__(“选择城市”、“粉碎插件”);
foreach($cities作为$city){
$select_city.=''.$city';
}
$select_city.=''。“\n”;
重置($城市);