Hiển thị 1–9 của 204 kết quả
/** * Fix 404 khi filter brand trên category archive * (WooCommerce Permalink Manager + TS Product Filter By Brand) */ add_action( 'pre_get_posts', function( $query ) { if ( is_admin() || ! $query->is_main_query() ) return; // Chỉ chạy khi có filter product_brand if ( ! isset( $_GET['product_brand'] ) ) return; // Nếu WordPress đang báo 404 nhưng thực ra là category if ( $query->is_404() ) { $slug = trim( parse_url( $_SERVER['REQUEST_URI'], PHP_URL_PATH ), '/' ); $slug = explode( '/', $slug ); $slug = end( $slug ); $term = get_term_by( 'slug', $slug, 'product_cat' ); if ( $term ) { $query->set_404(); // reset $query->is_404 = false; $query->is_tax = true; $query->is_archive = true; $query->set( 'product_cat', $slug ); $query->set( 'taxonomy', 'product_cat' ); $query->set( 'term', $slug ); status_header( 200 ); } } // Thêm tax_query cho brand filter if ( ( $query->is_tax( 'product_cat' ) || $query->get( 'product_cat' ) ) ) { $brand_ids = array_map( 'intval', explode( ',', sanitize_text_field( $_GET['product_brand'] ) ) ); $tax_query = $query->get( 'tax_query' ) ?: []; $tax_query[] = [ 'taxonomy' => 'product_brand', 'field' => 'term_id', 'terms' => $brand_ids, 'operator' => 'IN', ]; $query->set( 'tax_query', $tax_query ); } }, 5 ); // Flush rewrite rules một lần sau khi save add_action( 'init', function() { if ( get_option( 'druco_brand_filter_flushed' ) !== '1' ) { flush_rewrite_rules(); update_option( 'druco_brand_filter_flushed', '1' ); } });