Advertisement
niammuddin

menambah kategori a-z disemua postingan saat posting+edit

Oct 23rd, 2017
277
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.88 KB | None | 0 0
  1. <?php
  2. function pertaman($kata) {
  3. $sim = '-';
  4. $str = explode(' ', $kata);
  5. $initials = $str[0];
  6. if ($initials[0] == $sim) {
  7. $hasil = $initials[1];
  8. }
  9. else{
  10. $hasil = $initials[0];
  11. }
  12. return $hasil;
  13. }
  14. //echo pertaman('-dunia ini sangat indah');
  15.  
  16.  
  17.  
  18. add_action('save_post', 'add_title_as_category');
  19.  
  20. function add_title_as_category( $postid ) {
  21.   if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) return;
  22.   $post = get_post($postid);
  23.   if ( $post->post_type == 'post') { // change 'post' to any cpt you want to target
  24.     $term = get_term_by('slug', pertaman($post->post_name), 'category');
  25.     if ( empty($term) ) {
  26.       $add = wp_insert_term( pertaman($post->post_title), 'category', array('slug'=> pertaman($post->post_name)) );
  27.       if ( is_array($add) && isset($add['term_id']) ) {
  28.         wp_set_object_terms($postid, $add['term_id'], 'category', true );
  29.       }
  30.     }
  31.   }
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement