这几行对于wp_dropdown_categories
用于后端(edit.php)列表子选择的自定义分类法。因为列表中的值是term id
并且不可用于查询中的子选择。下面的代码部分用于管理自定义帖子类型和自定义分类的类。
add_action( \'restrict_manage_posts\', array(&$this,\'restrict_manage_writer_posts\') );
add_action( \'pre_get_posts\', array(&$this,\'wpse6066_pre_get_posts\' ) );
function restrict_manage_writer_posts () {
$selected = "";
if ( isset ( $_GET[\'writer_name\'] ) ) {
$selected = $_GET[\'writer_name\'];
}
$dropdown_options = array(
\'taxonomy\' => \'writer\',
\'show_option_all\' => __( \'View all writers\' ),
\'hide_empty\' => 0,
\'hierarchical\' => 1,
\'show_count\' => 0,
\'orderby\' => \'name\',
\'name\' => \'writer_name\',
\'selected\' => $selected
);
wp_dropdown_categories( $dropdown_options );
}
/**
* to fixes wp_dropdown_categories id value in option
* thanks to http://wordpress.stackexchange.com/questions/6066/query-custom-taxonomy-by-term-id
*/
function wpse6066_pre_get_posts( &$wp_query )
{
if ( $wp_query->is_tax ) { ;
if ( is_numeric( $wp_query->get( \'writer_name\' ) ) ) {
// Convert numberic terms to term slugs for dropdown
$term = get_term_by( \'term_id\', $wp_query->get( \'writer_name\' ), \'writer\' );
if ( $term ) {
$wp_query->set( \'writer_name\', $term->slug );
}
}
}
}