迟交的答复
更新WordPress 3.5+
我编写了以下插件来扩展
WP_List_Table
用于具有以下功能的内置和自定义帖子类型:
添加列(如果show_admin_column
设置为false
)添加分类单元,包括到表格单元格的链接添加一个下拉列表,显示分配给分类单元的帖子数量,以过滤显示的帖子,允许按多个分类法进行过滤(最好作为mu插件使用):
add_action( \'plugins_loaded\', array( \'WCM_Admin_PT_List_Tax_Filter\', \'init\' ) );
class WCM_Admin_PT_List_Tax_Filter
{
private static $instance;
public $post_type;
public $taxonomies;
public $new_cols = array();
static function init()
{
null === self :: $instance AND self :: $instance = new self;
return self :: $instance;
}
public function __construct()
{
add_action( \'load-edit.php\', array( $this, \'setup\' ) );
}
public function setup()
{
add_action( current_filter(), array( $this, \'setup_vars\' ), 20 );
add_action( \'restrict_manage_posts\', array( $this, \'get_select\' ) );
add_filter( "manage_taxonomies_for_{$this->post_type}_columns", array( $this, \'add_columns\' ) );
}
public function setup_vars()
{
$this->post_type = get_current_screen()->post_type;
$this->taxonomies = array_diff(
get_object_taxonomies( $this->post_type )
,get_taxonomies( array( \'show_admin_column\' => \'false\' ) )
);
}
public function add_columns( $taxonomies )
{
return array_merge(
$taxonomies
,$this->taxonomies
);
}
/**
* Select form element to filter the post list
* @return string HTML
*/
public function get_select()
{
$html = \'\';
foreach ( $this->taxonomies as $tax )
{
$options = sprintf(
\'<option value="">%s %s</option>\'
,__( \'View All\' )
,get_taxonomy( $tax )->label
);
$class = is_taxonomy_hierarchical( $tax ) ? \' class="level-0"\' : \'\';
foreach ( get_terms( $tax ) as $taxon )
{
$options .= sprintf(
\'<option %s%s value="%s">%s%s</option>\'
,isset( $_GET[ $tax ] ) ? selected( $taxon->slug, $_GET[ $tax ], false ) : \'\'
,\'0\' !== $taxon->parent ? \' class="level-1"\' : $class
,$taxon->slug
,\'0\' !== $taxon->parent ? str_repeat( \' \', 3 ) : \'\'
,"{$taxon->name} ({$taxon->count})"
);
}
$html .= sprintf(
\'<select name="%s" id="%s" class="postform">%s</select>\'
,$tax
,$tax
,$options
);
}
return print $html;
}
}
解释首先,我做了一个很棒的工作,但是做了大量的工作来获取自定义列(不知道
show_admin_column
). 然后我做了很多
parse_query
通过两种分类法对其进行筛选。幸运的是,我立即得到了一个令人惊讶的结果(没有添加代码)。查看以下转储示例:
public \'tax_query\' =>
object(WP_Tax_Query)[429]
public \'queries\' =>
array (size=2)
0 =>
array (size=5)
\'taxonomy\' => string \'post_format\' (length=11)
\'terms\' =>
array (size=1)
0 => string \'post-format-gallery\' (length=19)
\'include_children\' => boolean true
\'field\' => string \'slug\' (length=4)
\'operator\' => string \'IN\' (length=2)
1 =>
array (size=5)
\'taxonomy\' => string \'category\' (length=8)
\'terms\' =>
array (size=4)
0 => int 5
1 => int 6
2 => int 7
3 => int 8
\'include_children\' => boolean false
\'field\' => string \'term_id\' (length=7)
\'operator\' => string \'IN\' (length=2)
public \'relation\' => string \'AND\' (length=3)
WP已经默认这么做了!