• 01/03/2019 • 3 phút đọc • 11 lượt xem

Filtering Posts by Taxonomies in the Dashboard

Việt Lâm Coder
Việt Lâm Coder
Senior Fullstack & System Architect • Hotline: 0944.71.4545

By default, the admin Posts screen allows us to filter posts by the built-in “Categories” Taxonomy, using a neat, user-friendly dropdown menu. But in many cases, when we register new Taxonomies we often want to be able to filter posts by these new Taxonomies as well. In this quick tutorial, we will learn how to add new filters (dropdowns) to any Post Type screen in order to filter content by custom Taxonomies.

 

Filter WordPress Posts by Category
Filter WordPress Posts by Category

Registering a New Post Type and Taxonomies


For many WordPress developers this is a basic step, we do it in almost every project. Therefore, we won’t be getting into how to set it all up in your code — instead, use the following links to navigate to the snippets. You can use the Post Type Generator and the Taxonomy Generator to do that.

Ok, so in our example we will create a car catalog for an imaginary car retailer or agency. We will create a new “Car” post type and add several taxonomies (manufacturer, model, transmission, doors and color).

 

Cars Admin Screen
Cars Admin Screen

Adding Filters to the Post Type Admin Screen


Now that we have a working “Car” Post Type with several Taxonomies, we would like to be able to filter our cars in the admin area. For that we need to add filters (dropdowns) to help us sort our content and manage it easily.

By default, when we register new Taxonomies, WordPress does not add sorting filters to the admin area screen. However, developers are capable of doing that manually using the restrict_manage_posts action which fires just before the “Filter” button is printed out.

This much useful action has two parameters: The $post_type parameter accepts the Post Type’s slug and the $which parameter accepts the location of the navigation markup: ‘top’, ‘bottom’ (it also allows ‘bar’ for the Media screen).

For our example we will use the $post_type parameter to apply these filters only in the “Car” admin screen.

 


<?php
function filter_cars_by_taxonomies( $post_type, $which ) {

// Apply this only on a specific post type
if ( 'car' !== $post_type )
return;

// A list of taxonomy slugs to filter by
$taxonomies = array( 'manufacturer', 'model', 'transmission', 'doors', 'color' );

foreach ( $taxonomies as $taxonomy_slug ) {

// Retrieve taxonomy data
$taxonomy_obj = get_taxonomy( $taxonomy_slug );
$taxonomy_name = $taxonomy_obj->labels->name;

// Retrieve taxonomy terms
$terms = get_terms( $taxonomy_slug );

// Display filter HTML
echo "<select name='{$taxonomy_slug}' id='{$taxonomy_slug}' class='postform'>";
echo '<option value="">' . sprintf( esc_html__( 'Show All %s', 'text_domain' ), $taxonomy_name ) . '</option>';
foreach ( $terms as $term ) {
printf(
'<option value="%1$s" %2$s>%3$s (%4$s)</option>',
$term->slug,
( ( isset( $_GET[$taxonomy_slug] ) && ( $_GET[$taxonomy_slug] == $term->slug ) ) ? ' selected="selected"' : '' ),
$term->name,
$term->count
);
}
echo '</select>';
}

}
add_action( 'restrict_manage_posts', 'filter_cars_by_taxonomies' , 10, 2);


The Result


Now we have Taxonomy filters in our Cars admin screen, which allows us to display content that is assigned to the currently user-selected Taxonomies.

 

Filter Cars by Taxonomy
Filter Cars by Taxonomy

Hope this cool solution will help your clients manage their content!

Việt Lâm Coder

Việt Lâm Coder

Tác giả bài viết và chuyên gia quản trị website. Nếu bạn gặp khó khăn trong quá trình thực hiện hoặc có nhu cầu thiết kế website, lập trình web app Laravel, hãy liên hệ ngay với tôi qua Hotline / Zalo: 0944.71.4545.

☕ Mời Tác Giả Ly Cà Phê Ấm Lòng

Nếu bài viết này giúp ích cho công việc của bạn, hãy tiếp thêm động lực cho tác giả duy trì kho tri thức bằng một ly cà phê ấm lòng nhé!

Mã QR ủng hộ tác giả
Ngân hàng TMCP Quân Đội (MB Bank)TONG VIET LAM

Có Thể Bạn Quan Tâm

Cách Mở Tab Ẩn Danh Trên Safari MacBook Đơn Giản

Cách mở tab ẩn danh trên Safari MacBook sẽ giúp cho người dùng có thể bảo vệ quyền riêng tư và duyệt web một cách an toà...

Hướng dẫn 5 cách tắt update Windows 11 vĩnh viễn chi tiết nhất

Mặc định Windows 11 sẽ được tự động cập nhật trong nền. Tuy nhiên nếu vì lý do nào đó mà bạn không muốn nhận các bản...

Hướng dẫn trỏ file hosts cho domain trên hệ điều hành Windows

– Làm cách nào để 1 website có thể hoạt động trên Share hosting, VPS, Server mà không cần cấu hình các record DNS cho do...

Kích hoạt Cloudflare Turnstile trong Contact Form 7 chống spam

Cloudflare Turnstile là giải pháp xác thực không dùng CAPTCHA giúp bảo vệ website WordPress khỏi spam một cách nhẹ nhàng...

Cách Sử Dụng DeepSeek R1 Miễn Phí Trong Visual Studio Code Với Cline?

Hãy đối mặt với thực tế: Các trợ lý lập trình AI như GitHub Copilot thì tuyệt vời, nhưng chi phí đăng ký của chúng có th...

Gmail API – Tất cả những gì bạn cần biết - 2025

Nếu bạn đang tìm cách tích hợp Gmail vào các ứng dụng của riêng mình thì Gmail API chính là thứ bạn cần! Gmail là một lự...

Hướng dẫn tạo màn hình desktop ảo bằng Task View trên Surface Windows 11

Việt Lâm Coder sẽ hướng dẫn tạo lập nhiều màn hình desktop ảo bằng Task View trên Surface Windows 11 để sắp xếp các tác...

How to Transfer PuTTY Sessions To Another Windows Machine

his is an user contributed article. PuTTY is a terminal emulator application which can act as a client for the SSH, T...

Hướng dẫn cách kiểm tra cổng đang mở (Check open port) trên linux

Với nhiều năm kinh nghiệm hỗ trợ Khách hàng của Tenten, chúng tôi nhận thấy việc thường xuyên theo dõi kiểm tra hoạt độn...

Kiểm tra tất cả các port mà Cyber Panel đang cần có được mở hay không ?

Để kiểm tra tất cả các port cùng một lúc, bạn có thể sử dụng lệnh: netstat -tulpn | grep -E ':(8090|80|443|21|40110|401...

Hướng dẫn cài đặt KeyHelp Panel

Tóm tắt về KeyHelp Bạn đã có phần cứng tốt nhất cho các dự án Internet của mình nhưng việc quản lý phức tạp, hiệu suất...

Tổng hợp danh sách Control Panel VPS free tốt nhất hiện nay

Control Panel VPS free cung cấp một lựa chọn tuyệt vời cho những người dùng muốn quản lý VPS một cách dễ dàng và hiệ...