<?php
function pertaman($kata) {
$sim = '-';
$str = explode(' ', $kata);
$initials = $str[0];
if ($initials[0] == $sim) {
$hasil = $initials[1];
}
else{
$hasil = $initials[0];
}
return $hasil;
}
//echo pertaman('-dunia ini sangat indah');
add_action('save_post', 'add_title_as_category');
function add_title_as_category( $postid ) {
if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) return;
$post = get_post($postid);
if ( $post->post_type == 'post') { // change 'post' to any cpt you want to target
$term = get_term_by('slug', pertaman($post->post_name), 'category');
if ( empty($term) ) {
$add = wp_insert_term( pertaman($post->post_title), 'category', array('slug'=> pertaman($post->post_name)) );
if ( is_array($add) && isset($add['term_id']) ) {
wp_set_object_terms($postid, $add['term_id'], 'category', true );
}
}
}
}
Comments