HOME > WordPress> function.php

カスタムフィールドの値でカテゴリーを自動登録

function event_add_autoterm( $post_id  ) {
    global $post;
    $new_post = get_post( $post_id );
    $content = $new_post->post_content;
    if( $new_post->post_type == ‘event’ ) {
        
        $custom_fields = get_post_custom($post_id);
        if($custom_fields[‘kind_event’][0]==’会社’ ) {
            wp_set_object_terms( $post_id, ‘company’, ‘event-category’, false );
            
        }elseif($custom_fields[‘kind_event’][0]==’アドバイザー’ ) {
            $cat = $custom_fields[‘adviser’][0];
            wp_set_object_terms( $post_id, $cat, ‘event-category’, false );
        
    }elseif($custom_fields[‘kind_event’][0]==’個人’ and $custom_fields[‘スケジュール共有’] [0]== ‘共有しない’) {
            //投稿者のスラッグを取得
            $author = $new_post->post_author;
            $author = get_userdata($author);
            $name = $author->user_login.’2′;
        wp_set_object_terms( $post_id, $name, ‘event-category’, false );
        }
            
    }
}
add_action( ‘save_post’, ‘event_add_autoterm’ );