多亏了@cybmeta,我才能够解决我的问题。下面我发布了所有相关代码。请记住,它指的是使用4种自定义分类法(“意大利”、“法国”、“西班牙”、“世界”)的“分销商”自定义帖子类型。
// add columns
function add_distributor_columns($table_columns){
$area_column = array(\'area\' => \'Area\');
$macroarea_column = array(\'macroarea\' => \'Macroarea\');
// put them on the right position
$table_columns = array_slice($table_columns, 0, 2, true) + $area_column + $macroarea_column + array_slice($table_columns, 2, NULL, true);
return $table_columns;
}
add_filter(\'manage_distributor_posts_columns\', \'add_distributor_columns\', 10);
// add contents to columns
function add_contents_to_distributor_columns($column_name, $id){
global $post;
$post_id = $post->ID;
if($column_name == \'area\'){
$italy_areas = (array) get_the_terms($post_id,\'italy\');
$france_areas = (array) get_the_terms($post_id,\'france\');
$spain_areas = (array) get_the_terms($post_id,\'spain\');
$world_areas = (array) get_the_terms($post_id,\'world\');
$areas = $italy_areas + $france_areas + $spain_areas + $world_areas;
if(!empty($areas) && $areas[0] !== false){
$areas_names = array();
foreach($areas as $area){
$area_name = $area->name;
$areas_names[] = $area_name;
}
echo implode(\', \',$areas_names);
}
}//end if
if($column_name == \'macroarea\'){
$macroareas = array();
if(has_taxonomy(\'italy\', $post_id)) $macroareas[] = \'Italia\';
if(has_taxonomy(\'france\', $post_id)) $macroareas[] = \'Francia\';
if(has_taxonomy(\'spain\', $post_id)) $macroareas[] = \'Spagna\';
if(has_taxonomy(\'world\', $post_id)) $macroareas[] = \'Mondo\';
if(!empty($macroareas)) echo implode(\', \',$macroareas);
}//end if
}// add_contents_to_distributor_columns
add_action(\'manage_distributor_posts_custom_column\', \'add_contents_to_distributor_columns\', 10, 2);
// add macroarea filter
function add_filter_by_macroarea() {
global $typenow;
$post_type = \'distributor\';
$macroaree = array(\'all\',\'italy\',\'france\',\'spain\',\'world\');
$selected = isset($_GET[\'macroarea\']) ? $_GET[\'macroarea\'] : \'\';
if($typenow == $post_type) {
$html = \'<select id="filter-by-macroarea" name="macroarea">\';
foreach($macroaree as $macroarea){
$s = ($macroarea == $selected) ? \' selected="selected"\' : \'\';
$html .= \'<option value="\' . $macroarea . \'"\' . $s . \'>\' . $macroarea . \'</option>\';
}
echo $html .= \'</select>\';
};
}
add_action(\'restrict_manage_posts\', \'add_filter_by_macroarea\');
// add contents to filtered list
function convert_id_to_term_in_query($query){
global $pagenow, $post_type;
if($pagenow == \'edit.php\' && $post_type == \'distributor\'){
$selected = isset($_GET[\'macroarea\']) ? $_GET[\'macroarea\'] : null;
if($selected && $selected != \'all\'){
$terms_ids = wp_list_pluck(get_terms($selected), \'term_id\');
$query->query_vars[\'tax_query\'] = array(
array(
\'taxonomy\' => $selected,
\'field\' => \'term_id\',
\'terms\' => $terms_ids,
)
);
}// if selected
}// if right page
}// end filter
add_filter(\'parse_query\', \'convert_id_to_term_in_query\');
// UTILITY FUNCTIONS ---------- //
function get_taxonomies_info($post_id = null){
if(!$post_id){ // if not passed, try to get the ID
global $wp_query;
$post_id = $wp_query->get_queried_object_id();//current post id
}
global $wpdb;
return $wpdb->get_results($wpdb->prepare("
select tax.taxonomy as taxonomy, group_concat(tr.name) as term_names, group_concat(tr.term_id) as term_ids
from " . $wpdb->prefix . "term_taxonomy tax
INNER JOIN " . $wpdb->prefix . "term_relationships rel ON tax.term_taxonomy_id =rel.term_taxonomy_id
INNER JOIN " . $wpdb->prefix . "terms tr ON tr.term_id = tax.term_id
WHERE rel.object_id = %d
GROUP BY taxonomy
",$post_id),OBJECT_K);
}
function has_taxonomy($taxonomy_slug, $post_id = null){
if(!$post_id){// if not passed, try to get the ID
global $wp_query;
$post_id = $wp_query->get_queried_object_id();//current post id
}
return array_key_exists($taxonomy_slug, get_taxonomies_info($post_id));
}