niammuddin icon

menambah kategori a-z disemua postingan saat posting+edit

niammuddin | PRO | 10/23/17 06:43:29 PM UTC | 0 ⭐ | 734 👁️ | Never ⏰ | []
PHP |

880 B

|

None

|

0 👍

/

0 👎

<?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