Advertisement
niammuddin

menambah kategori a-z disemua postingan wordpress

Oct 23rd, 2017
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.21 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.  
  15. // untuk ambil data postingan
  16. $postid = $array_wp[0]['ID'];
  17. $posttitle = pertaman($array_wp[0]['post_title']);
  18. $term = get_term_by('slug', $posttitle, 'category');
  19. $cat_name = $term->name;
  20.  
  21. // untuk mengecek apakah sudah ada kategori berdasarkan huruf pertaman dari title posts
  22. $parent_term = term_exists(pertaman($array_wp[0]['post_title']),'category'); // array is returned if taxonomy is given
  23. $cat_exists = $parent_term['term_id']; // get numeric term id
  24.  
  25. if (empty($cat_exists)) {
  26. // untuk membuat kategori baru ketika kategori belum ada, ketika sudah ada ini tidak akan bekerja
  27. wp_insert_term( pertaman($array_wp[0]['post_title']), 'category', array('slug' => pertaman($array_wp[0]['post_title'])) );
  28. }
  29.  
  30. // untuk menambah kategori ke dalam postingan
  31. $term_taxonomy_ids = wp_set_object_terms( $postid, $cat_name, 'category', true );
  32. if ( is_wp_error( $term_taxonomy_ids ) ) {
  33.   // There was an error somewhere and the terms couldn't be set.
  34. } else {
  35.   // Success! These categories were added to the post.
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement