Skip to content

Hi You, I am Tade

  • About
  • Donate
  • Privacy policy
    • About
    • Donate
    • Privacy policy

Thêm bộ lọc taxonomy trong admin custom post type

  • Hoàng Thức
  • May 28, 2017

Đây là đoạn code nhỏ dùng để tạo bộ lọc custom taxonomy trong admin custom post type CMS wordpress. Để có thể dễ dàng quản lý bài viết trên site. Giao diện như bên dưới.

Vẫn như củ các bạn copy code và đặt vào file functions.php thôi nhưng chú ý 1 số thông số như sau:

1.Hook : Mình sử dung 2 hook là restrict_manage_posts và parse_query.

restrict_manage_posts để lấy data taxonomy và hiển thị ra dạng dropdown taxonomy

parse_query cải tạo lại bộ lọc để nó có thể filter thêm cái taxonomy mình mới thêm.

2.Biến: Bạn thay biến post_type và taxonomy thành của mình để sử dụng nhé.

3.Code đây


add_action('restrict_manage_posts', 'tsm_filter_post_type_by_taxonomy');
function tsm_filter_post_type_by_taxonomy() {
 global $typenow;
 $post_type = 'project'; // change to your post type
 $taxonomy = 'kind'; // change to your taxonomy
 if ($typenow == $post_type) {
 $selected = isset($_GET[$taxonomy]) ? $_GET[$taxonomy] : '';
 $info_taxonomy = get_taxonomy($taxonomy);
 wp_dropdown_categories(array(
 'show_option_all' => __("Lọc {$info_taxonomy->label}"),
 'taxonomy' => $taxonomy,
 'name' => $taxonomy,
 'orderby' => 'name',
 'selected' => $selected,
 'show_count' => true,
 'hide_empty' => true,
 ));
 };
}
/**
 * Filter posts by taxonomy in admin
 * @author Hoang Thuc
 * @link https://hoangthuc.com/bo-loc-taxonomy-trong-admin-custom-post
 */
add_filter('parse_query', 'tsm_convert_id_to_term_in_query');
function tsm_convert_id_to_term_in_query($query) {
 global $pagenow;
 $post_type = 'project'; // change to your post type
 $taxonomy = 'kind'; // change to your taxonomy
 $q_vars = &$query->query_vars;
 if ( $pagenow == 'edit.php' && isset($q_vars['post_type']) && $q_vars['post_type'] == $post_type && isset($q_vars[$taxonomy]) && is_numeric($q_vars[$taxonomy]) && $q_vars[$taxonomy] != 0 ) {
 $term = get_term_by('id', $q_vars[$taxonomy], $taxonomy);
 $q_vars[$taxonomy] = $term->slug;
 }
} 

Chúc các bạn thành công !

    Tags:
  • Taxonomy filter admin custom post
    Share

Post navigation

←Previous:
Next:→

47 thoughts on “Thêm bộ lọc taxonomy trong admin custom post type”

  1. Pingback: שרת אחסון אתרים
  2. Pingback: SEO Affiliate Domination
  3. Pingback: กระเบื้องยาง SPC ราคา
  4. Pingback: 6 เกม PG Slot ที่มีชื่อเสียงและนิยมเล่น
  5. Pingback: steenslagfolie
  6. Pingback: ECs
  7. Pingback: John Lobb
  8. Pingback: Sylfirm
  9. Pingback: รับทำเว็บไซต์
  10. Pingback: ส่งพัสดุ
  11. Pingback: โคมไฟ
  12. Pingback: dark168
  13. Pingback: บาคาร่าเกาหลี
  14. Pingback: www.nexusmarket-wiki.com
  15. Pingback: sci/sci news/news sci/ science diyala
  16. Pingback: สล็อตออนไลน์ เว็บตรงไม่ผ่านเอเย่นต์
  17. Pingback: pgslot168
  18. Pingback: online chat
  19. Pingback: north disposable vape how to unclog disposable thc vape
  20. Pingback: ลงทุนแฟรนไชส์
  21. Pingback: เช่ารถตู้พร้อมคนขับ
  22. Pingback: เค้กด่วน
  23. Pingback: เค้กวันเกิด
  24. Pingback: som777
  25. Pingback: ติดเน็ตบ้าน AIS
  26. Pingback: best gym equipment store
  27. Pingback: ยิง sms
  28. Pingback: ไฟสนามกีฬา
  29. Pingback: Aviation Tire
  30. Pingback: sagame
  31. Pingback: ทัวร์ยุโรป
  32. Pingback: husqvarna veikals
  33. Pingback: lg96
  34. Pingback: ปั้มฟอล
  35. Pingback: pgslot
  36. Pingback: Mostbet PL
  37. Pingback: เว็บพนันออนไลน์เกาหลี
  38. Pingback: ram for gaming
  39. Pingback: เกมไพ่ ออนไลน์ ครบเครื่องเรื่องความบันเทิง
  40. Pingback: สล็อตเว็บใหญ่ รวมทุกค่าย เว็บตรงของแทร่ LSM99
  41. Pingback: visit the website
  42. Pingback: fortunerabbitjogar.com
  43. Pingback: บาคาร่า ufa11k
  44. Pingback: ร้านเค้กวันเกิดใกล้ฉัน
  45. Pingback: mmabet
  46. Pingback: Kraken Hunter
  47. Pingback: บ้านพักคนชรา

Comments are closed.

©2025 Hi You, I am Tade / Designed & Built by WPFound