Xác định taxonomy (chuyên mục) cần lọc:

vd: Taxonomy của bài viết là: category, post_tag

Để lấy taxonomy của custom post type  bất kỳ thì bạn hãy làm theo các bước sau:

Hình ảnh bên dưới post type là: wpb_fp_portfolio và taxonomy là: wpb_fp_portfolio_cat

Viết hàm lọc taxonomy cho custom post type

add_action( 'restrict_manage_posts', 'wpb_fp_portfolio_filter', 10, 2 );
function wpb_fp_portfolio_filter( $post_type, $which ) {
    if ( 'wpb_fp_portfolio' !== $post_type ) {
        return; //check to make sure this is your cpt
    }
 
    $taxonomy_slug = 'wpb_fp_portfolio_cat';
    $taxonomy      = get_taxonomy($taxonomy_slug);
    $selected      = '';
    $request_attr  = 'portfolio_category'; //this will show up in the url
  
    if ( isset( $_REQUEST[ $request_attr ] ) ) {
        $selected = $_REQUEST[ $request_attr ]; //in case the current page is already filtered
    }
     
    wp_dropdown_categories(array(
        'show_option_all' =>  __("Show All {$taxonomy->label}"),
        'taxonomy'        =>  $taxonomy_slug,
        'name'            =>  $request_attr,
        'orderby'         =>  'name',
        'selected'        =>  $selected,
        'hierarchical'    =>  true,
        'depth'           =>  3,
        'show_count'      =>  true, // Show number of post in parent term
        'hide_empty'      =>  false, // Don't show posts w/o terms
    ) );
}

 

Bài viết liên quan

post-no-image

Chia sẻ một số thư viện loading css đẹp

giai-phap-chinh-css-khong-bi-luu-cache-trinh-duyet

Giải pháp tốt khi chỉnh sửa css mà không sợ bị lưu cache trình duyệt

post-no-image

Một số css tùy biến thẻ input type: select, number

post-no-image

Tắt lazyload cho một số hình ảnh

post-no-image

Tuỳ biến layout YITH WooCommerce Wishlist

post-no-image

Một số hook hoặc filter thường sử dụng trong plugin WPML