您不必重建元数据库。。。您可以通过get_terms
滤器这将向复选框列表中添加一个“所有术语”术语(假设采用层次分类法)。
add_filter( \'get_terms\', \'wpa104168_all_terms\', 10, 3 );
function wpa104168_all_terms ( $terms, $taxonomies, $args ){
if ( is_admin() && function_exists( \'get_current_screen\' ) && ! is_wp_error( $screen = get_current_screen() ) && in_array( $screen->base, array( \'post\', \'edit-post\', \'edit\' ) ) ) {
if( in_array( \'genre\', ( array ) $taxonomies ) ) {
$all_terms = __( \'All Genres\' );
$all = (object) array( \'term_id\' => \'all\', \'slug\' => \'all\', \'name\' => $all_terms, \'parent\' => \'0\' );
$terms[\'all\'] = $all;
}
}
return $terms;
}
这一点继续下去
save_post
测试是否存在该伪术语,然后将该分类法中的所有术语分配给该特定帖子。
add_action( \'save_post\', \'wpa104168_save_all_terms\', 10, 3 );
function wpa104168_save_all_terms ( $post_id ){
// verify this came from our screen and with proper authorization.
if ( !wp_verify_nonce( $_POST[\'_wpnonce\'], \'update-post_\' . $post_id )) {
return $post_id;
}
// verify if this is an auto save routine. If it is our form has not been submitted, so we dont want to do anything
if ( defined(\'DOING_AUTOSAVE\') && DOING_AUTOSAVE )
return $post_id;
// Check permissions
if ( \'page\' == $_POST[\'post_type\'] ) {
if ( !current_user_can( \'edit_page\', $post_id ) )
return $post_id;
} else {
if ( !current_user_can( \'edit_post\', $post_id ) )
return $post_id;
}
// OK, we\'re authenticated: we need to find and save the data
if ( isset( $_POST[\'tax_input\'][\'genre\'] ) && is_array( $_POST[\'tax_input\'][\'genre\'] ) && in_array( \'all\', $_POST[\'tax_input\'][\'genre\'] ) ){
$args = array( \'hide_empty\' => false );
$terms = get_terms( \'genre\', $args );
if ( ! is_wp_error( $terms ) ){
foreach ( $terms as $term ){
$update[] = $term->slug;
}
wp_set_object_terms( $post_id, $update, \'genre\' );
}
}
return $post_id;
}
但脚本化解决方案要简单得多。不需要在每个管理页面上加载,但我会让你自己来整理。
澄清:变更genrechecklist
到相应的分类名称。。。。{$taxonomy}checklist
add_action( \'admin_print_footer_scripts\', \'wpa104168_js_solution\' );
function wpa104168_js_solution(){ ?>
<script type="text/javascript">
jQuery(document).ready(function($) {
$(\'ul#genrechecklist\').append(\'<li><label class="selectit"><input type="checkbox" class="toggle-all-terms"/> Check All</label>\');
$(\'.toggle-all-terms\').on(\'change\', function(){
$(this).closest(\'ul\').find(\':checkbox\').prop(\'checked\', this.checked );
});
});
</script>
<?php }