Hướng dẫn sử dụng các filter trong Init Live Search

Plugin Init Live Search hỗ trợ nhiều filter giúp bạn can thiệp và tùy biến kết quả tìm kiếm theo nhu cầu. Dưới đây là danh sách các filter chính cùng ví dụ sử dụng.

Hướng dẫn sử dụng các filter trong Init Live Search

1. init_plugin_suite_live_search_enable_fallback

Cho phép bật hoặc tắt cơ chế fallback (gợi ý bằng cách cắt bớt hoặc tạo bigram) nếu kết quả tìm kiếm ban đầu quá ít.

  • Tham số: (bool $enable, string $term, array $args)
  • Mặc định: Bật fallback nếu số kết quả ban đầu nhỏ hơn một nửa max_results.
add_filter('init_plugin_suite_live_search_enable_fallback', function($enable, $term, $request) {
    // Vô hiệu hóa fallback nếu từ khóa chứa số
    return !preg_match('/\d/', $term);
}, 10, 3);

2. init_plugin_suite_live_search_post_ids

Cho phép thay đổi danh sách ID bài viết được truy vấn từ DB (sau khi tìm kiếm và fallback nếu có).

  • Tham số: (array $post_ids, string $term, array $args)
add_filter('init_plugin_suite_live_search_post_ids', function($post_ids, $term, $args) {
    return array_filter($post_ids, function($post_id) {
        return !has_category(5, $post_id);
    });
}, 10, 3);

3. init_plugin_suite_live_search_result_item

Cho phép tùy biến từng phần tử kết quả trước khi trả về frontend.

  • Tham số: (array $item, int $post_id, string $term, array $args)
add_filter('init_plugin_suite_live_search_result_item', function($item, $post_id, $term, $args) {
    $item['author'] = get_post_meta($post_id, 'author_name', true);
    return $item;
}, 10, 4);

4. init_plugin_suite_live_search_results

Tùy chỉnh mảng kết quả cuối cùng trước khi trả về qua REST API.

  • Tham số: (array $results, array $post_ids, string $term, array $args)
add_filter('init_plugin_suite_live_search_results', function($results, $post_ids, $term, $args) {
    $results[] = [
        'title' => 'Tổng kết',
        'type'  => 'Thông báo',
        'url'   => '#',
        'thumb' => '',
        'date'  => '',
        'note'  => 'Có tổng cộng ' . count($post_ids) . ' kết quả.'
    ];
    return $results;
}, 10, 4);

5. init_plugin_suite_live_search_category

Tùy biến tên danh mục hiển thị trên kết quả tìm kiếm.

  • Tham số: (string $category_name, int $post_id)
add_filter('init_plugin_suite_live_search_category', function($cat, $post_id) {
    return strtoupper($cat);
}, 10, 2);

6. init_plugin_suite_live_search_default_thumb

Thay đổi ảnh thumbnail mặc định nếu bài viết không có ảnh đại diện.

  • Tham số: (string $thumb_url)
add_filter('init_plugin_suite_live_search_default_thumb', function($url) {
    return get_stylesheet_directory_uri() . '/img/default-thumb.jpg';
});

7. init_plugin_suite_live_search_query_args

Tùy chỉnh tham số WP_Query cho các command như /recent, /date, /tax, /product, /random.

  • Tham số: (array $args, string $type, WP_REST_Request $request)
add_filter('init_plugin_suite_live_search_query_args', function($args, $type, $request) {
    if ($type === 'recent') {
        $args['meta_query'][] = [
            'key' => '_custom_flag',
            'value' => 'yes'
        ];
    }
    return $args;
}, 10, 3);

8. init_plugin_suite_live_search_taxonomy_cache_ttl

Tùy chỉnh thời gian lưu cache (tính bằng giây) của endpoint /taxonomies. Trả về 0 nếu muốn tắt cache hoàn toàn.

  • Tham số: (int $ttl, string $taxonomy, int $limit)
  • Mặc định: 300 giây (5 phút).
add_filter('init_plugin_suite_live_search_taxonomy_cache_ttl', function($ttl, $taxonomy, $limit) {
    // Tắt cache cho taxonomy 'post_tag'
    if ($taxonomy === 'post_tag') return 0;
    // Rút ngắn TTL cho các taxonomy khác nếu limit nhỏ
    if ($limit <= 5) return 60;
    return $ttl;
}, 10, 3);

9. init_plugin_suite_live_search_stop_single_words

Loại bỏ các từ đơn vô nghĩa trước khi phân tích tiêu đề và tạo bigram.

  • Tham số: (array $stop_words, string $locale)
add_filter('init_plugin_suite_live_search_stop_single_words', function($words, $locale) {
    if (strpos($locale, 'vi') === 0) {
        $words[] = 'tèo';  // thêm từ cần loại bỏ
    }
    return $words;
}, 10, 2);

10. init_plugin_suite_live_search_stop_words

Loại bỏ các cụm từ 2 từ (bigrams) vô nghĩa khỏi danh sách từ khóa được tạo từ tiêu đề.

  • Tham số: (array $stop_phrases, string $locale)
add_filter('init_plugin_suite_live_search_stop_words', function($phrases, $locale) {
    if ($locale === 'vi') {
        $phrases[] = 'đây là';
    }
    return $phrases;
}, 10, 2);

11. init_plugin_suite_live_search_filter_lang

Lọc lại danh sách post ID theo ngôn ngữ hiện tại, hỗ trợ WPML hoặc Polylang.

  • Tham số: (array $post_ids, string $term, array $args)
// Polylang
add_filter('init_plugin_suite_live_search_filter_lang', function($post_ids, $term, $args) {
    if (!function_exists('pll_get_post_language')) return $post_ids;
    $lang = $args['lang'] ?? null;
    if (!$lang) return $post_ids;

    return array_filter($post_ids, fn($id) => pll_get_post_language($id) === $lang);
}, 10, 3);

// WPML
add_filter('init_plugin_suite_live_search_filter_lang', function($post_ids, $term, $args) {
    if (!function_exists('icl_object_id')) return $post_ids;
    $lang = $args['lang'] ?? null;
    if (!$lang || empty($post_ids)) return $post_ids;

    global $wpdb;
    $ids_in_lang = $wpdb->get_col($wpdb->prepare(
        "
        SELECT element_id FROM {$wpdb->prefix}icl_translations
        WHERE element_type LIKE 'post%%'
        AND language_code = %s
        AND element_id IN (" . implode(',', array_map('absint', $post_ids)) . ")
        ",
        $lang
    ));

    return array_map('absint', $ids_in_lang);
}, 10, 3);

12. init_plugin_suite_live_search_category_taxonomy

Tùy chỉnh taxonomy dùng để hiển thị tên danh mục trong kết quả tìm kiếm.

  • Tham số: (string $taxonomy, int $post_id)
  • Mặc định: category
add_filter('init_plugin_suite_live_search_category_taxonomy', function($taxonomy, $post_id) {
    if (get_post_type($post_id) === 'product') {
        return 'product_cat';
    }
    return $taxonomy;
}, 10, 2);

13. init_plugin_suite_live_search_seo_meta_keys

Tùy chỉnh danh sách các meta key dùng để tìm kiếm trong tiêu đề SEO và mô tả SEO.

  • Tham số: (array $meta_keys)
  • Mặc định: Bao gồm các meta key của Yoast, Rank Math, AIOSEO, TSF, SEOPress
add_filter('init_plugin_suite_live_search_seo_meta_keys', function($keys) {
    // Thêm meta key tùy chỉnh
    $keys[] = '_custom_seo_title';
    $keys[] = '_custom_seo_description';
    return $keys;
});

14. init_plugin_suite_live_search_weights

Tùy chỉnh trọng số của từng nguồn dữ liệu (tiêu đề, mô tả SEO, tag, v.v.) khi gộp và sắp xếp kết quả tìm kiếm.

  • Tham số: (array $weights, string $search_mode)
  • Mặc định: Tùy theo chế độ tìm kiếm:
    • title: [3, 2] (tiêu đề > SEO)
    • title_excerpt: [3, 2] (tiêu đề/mô tả > SEO)
    • title_tag: [3, 2, 1, 1] (tiêu đề > SEO > tag > tag từng từ)
add_filter('init_plugin_suite_live_search_weights', function($weights, $mode) {
    if ($mode === 'title_tag') {
        // Ưu tiên cực mạnh cho tiêu đề, giảm SEO và tag
        return [5, 2, 1, 1];
    }
    return $weights;
}, 10, 2);

15. init_plugin_suite_live_search_commands

Cho phép đăng ký thêm slash command tùy chỉnh, để hiển thị trong danh sách lệnh và xử lý bằng JavaScript hoặc REST API riêng.

  • Tham số: (array $commands, array $options)
add_filter('init_plugin_suite_live_search_commands', function($commands) {
    $commands['vip'] = __('Show VIP-only posts', 'my-theme');
    $commands['my'] = __('Show my posts', 'my-theme');
    return $commands;
});

Các lệnh được thêm vào sẽ tự động hiển thị trong hộp gợi ý khi gõ /, nhưng không được xử lý bởi plugin gốc. Bạn cần tự lắng nghe sự kiện JavaScript (ils:search-started, ils:results-loaded) hoặc triển khai endpoint riêng nếu cần.

Tổng kết

Hệ thống filter mạnh mẽ giúp Init Live Search dễ dàng mở rộng và tích hợp sâu vào theme hoặc plugin tùy chỉnh. Từ việc thay đổi kết quả đến tối ưu UX/UI, bạn hoàn toàn làm chủ plugin mà không cần đụng vào core code.

Bình luận


  • Không có bình luận.