WordPressのカスタムタクソノミーにデフォルトを設定してみる

カスタムタクソノミーにデフォルトを設定

WordPressのカスタムタクソノミーは、カテゴリー以外にも様々な分類を追加できて便利ではあるのですが、たまにカテゴリーのように、デフォルトの設定ができたら便利だと思うことはありませんか?

本日は、コピー&ペーストだけで使えるそんな便利コードをご紹介。

という訳で、CODE 1をテーマのfunctions.phpに追加すると、投稿設定画面に、投稿タイプ毎に登録されているタクソノミーのデフォルトをプルダウンより選択できるようになります。

あとは、投稿時にタクソノミーが選択されていなければ、初期設定で選んだ分類が自動的に設定されるようになります。

CODE 1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
function add_default_term_setting_item() {
    $post_types = get_post_types( array( 'public' => true, 'show_ui' => true ), false );
    if ( $post_types ) {
        foreach ( $post_types as $post_type_slug => $post_type ) {
            $post_type_taxonomies = get_object_taxonomies( $post_type_slug, false );
            if ( $post_type_taxonomies ) {
                foreach ( $post_type_taxonomies as $tax_slug => $taxonomy ) {
                    if ( ! ( $post_type_slug == 'post' && $tax_slug == 'category' ) && $taxonomy->show_ui ) {
                        add_settings_field( $post_type_slug . '_default_' . $tax_slug, $post_type->label . '用' . $taxonomy->label . 'の初期設定' , 'default_term_setting_field', 'writing', 'default', array( 'post_type' => $post_type_slug, 'taxonomy' => $taxonomy ) );
                    }
                }
            }
        }
    }
}
add_action( 'load-options-writing.php', 'add_default_term_setting_item' );
 
 
function default_term_setting_field( $args ) {
    $option_name = $args['post_type'] . '_default_' . $args['taxonomy']->name;
    $default_term = get_option( $option_name );
    $terms = get_terms( $args['taxonomy']->name, 'hide_empty=0' );
    if ( $terms ) :
?>
    <select name="<?php echo $option_name; ?>">
        <option value="0">設定しない</option>
<?php foreach ( $terms as $term ) : ?>
        <option value="<?php echo esc_attr( $term->term_id ); ?>"<?php echo $term->term_id == $default_term ? ' selected="selected"' : ''; ?>><?php echo esc_html( $term->name ); ?></option>
<?php endforeach; ?>
    </select>
<?php
    else:
?>
    <p><?php echo esc_html( $args['taxonomy']->label ); ?>が登録されていません。</p>
<?php
    endif;
}
 
 
function allow_default_term_setting( $whitelist_options ) {
    $post_types = get_post_types( array( 'public' => true, 'show_ui' => true ), false );
    if ( $post_types ) {
        foreach ( $post_types as $post_type_slug => $post_type ) {
            $post_type_taxonomies = get_object_taxonomies( $post_type_slug, false );
            if ( $post_type_taxonomies ) {
                foreach ( $post_type_taxonomies as $tax_slug => $taxonomy ) {
                    if ( ! ( $post_type_slug == 'post' && $tax_slug == 'category' ) && $taxonomy->show_ui ) {
                        $whitelist_options['writing'][] = $post_type_slug . '_default_' . $tax_slug;
                    }
                }
            }
        }
    }
    return $whitelist_options;
}
add_filter( 'whitelist_options', 'allow_default_term_setting' );
 
 
function add_post_type_default_term( $post_id, $post ) {
    if ( ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) || $post->post_status == 'auto-draft' ) { return; }
    $taxonomies = get_object_taxonomies( $post, false );
    if ( $taxonomies ) {
        foreach ( $taxonomies as $tax_slug => $taxonomy ) {
            $default = get_option( $post->post_type . '_default_' . $tax_slug );
            if ( ! ( $post->post_type == 'post' && $tax_slug == 'category' ) && $taxonomy->show_ui && $default && ! ( $terms = get_the_terms( $post_id, $tax_slug ) ) ) {
                if ( $taxonomy->hierarchical ) {
                    $term = get_term( $default, $tax_slug );
                    if ( $term ) {
                        wp_set_post_terms( $post_id, array_filter( array( $default ) ), $tax_slug );
                    }
                } else {
                    $term = get_term( $default, $tax_slug );
                    if ( $term ) {
                        wp_set_post_terms( $post_id, $term->name, $tax_slug );
                    }
                }
            }
        }
    }
}
add_action( 'wp_insert_post', 'add_post_type_default_term', 10, 2 );

「WordPressのカスタムタクソノミーにデフォルトを設定してみる」への2件のフィードバック

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です