Using Filters in Init Live Search

The Init Live Search plugin supports a variety of filters, allowing you to customize and extend search results as needed. Below is a list of key filters with usage examples.

Using Filters in Init Live Search

1. init_plugin_suite_live_search_enable_fallback

Enables or disables the fallback mechanism (e.g. trimming or bigram suggestions) when initial search results are too few.

  • Parameters: (bool $enable, string $term, array $args)
  • Default: Fallback is enabled if initial results are less than half of max_results.
add_filter('init_plugin_suite_live_search_enable_fallback', function($enable, $term, $request) {
    // Disable fallback if the keyword contains a number
    return !preg_match('/\d/', $term);
}, 10, 3);

2. init_plugin_suite_live_search_post_ids

Modify the list of post IDs retrieved from the DB (after search and fallback).

  • Parameters: (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

Customize each search result item before it’s returned to the frontend.

  • Parameters: (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

Modify the final result array before it’s returned via REST API.

  • Parameters: (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' => 'Summary',
        'type'  => 'Notice',
        'url'   => '#',
        'thumb' => '',
        'date'  => '',
        'note'  => 'Total results: ' . count($post_ids)
    ];
    return $results;
}, 10, 4);

5. init_plugin_suite_live_search_category

Customize the category name shown in search results.

  • Parameters: (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

Change the default thumbnail image if a post has no featured image.

  • Parameters: (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

Customize WP_Query arguments for commands like /recent, /date, /tax, /product, /random.

  • Parameters: (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

Set cache time (in seconds) for the /taxonomies endpoint. Return 0 to disable caching.

  • Parameters: (int $ttl, string $taxonomy, int $limit)
  • Default: 300 seconds (5 minutes).
add_filter('init_plugin_suite_live_search_taxonomy_cache_ttl', function($ttl, $taxonomy, $limit) {
    if ($taxonomy === 'post_tag') return 0;
    if ($limit <= 5) return 60;
    return $ttl;
}, 10, 3);

9. init_plugin_suite_live_search_stop_single_words

Remove meaningless single words before analyzing titles or generating bigrams.

  • Parameters: (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';
    }
    return $words;
}, 10, 2);

10. init_plugin_suite_live_search_stop_words

Remove meaningless two-word phrases (bigrams) from the keyword list.

  • Parameters: (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

Filter the post ID list based on current language – supports WPML and Polylang.

  • Parameters: (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

Customize the taxonomy used to get the category name in search results.

  • Parameters: (string $taxonomy, int $post_id)
  • Default: 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

Customize the list of SEO meta keys used in title/description search.

  • Parameters: (array $meta_keys)
  • Default: Includes meta keys from Yoast, Rank Math, AIOSEO, TSF, SEOPress.
add_filter('init_plugin_suite_live_search_seo_meta_keys', function($keys) {
    $keys[] = '_custom_seo_title';
    $keys[] = '_custom_seo_description';
    return $keys;
});

14. init_plugin_suite_live_search_weights

Customize weight scoring (title, SEO, tags, etc.) when ranking search results.

  • Parameters: (array $weights, string $search_mode)
  • Default: Varies by mode:
    • title: [3, 2] (title > SEO)
    • title_excerpt: [3, 2]
    • title_tag: [3, 2, 1, 1] (title > SEO > tag > individual tag)
add_filter('init_plugin_suite_live_search_weights', function($weights, $mode) {
    if ($mode === 'title_tag') {
        return [5, 2, 1, 1];
    }
    return $weights;
}, 10, 2);

15. init_plugin_suite_live_search_commands

Register custom slash commands, shown in suggestions and handled via JS or custom endpoints.

  • Parameters: (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;
});

These commands appear in the slash suggestion list but are not handled by the core plugin. You’ll need to listen to JS events (ils:search-started, ils:results-loaded) or build your own REST API endpoint.

16. init_plugin_suite_live_search_synonym_map

Customize or extend the list of synonyms used to expand search results when direct matches are not found.

  • Params: (array $synonym_map)
  • Default: Based on the Synonyms tab in admin settings — e.g. { "reaction": ["tương tác", "phản hồi"] }
add_filter('init_plugin_suite_live_search_synonym_map', function($map) {
    $map['speed'] = ['velocity', 'momentum'];
    $map['ai'] = ['artificial intelligence', 'machine learning'];
    return $map;
});

17. init_plugin_suite_live_search_smart_post_thumbnail_alt

Customize the auto-generated alt text for post thumbnails when no media alt is set — improves SEO and accessibility.

  • Params: (string $alt, int $post_id)
  • Default: Uses alt from the Media Library if available, otherwise generates one from the post title.
add_filter('init_plugin_suite_live_search_smart_post_thumbnail_alt', function($alt, $post_id) {
    return 'Thumbnail image for post #' . $post_id;
}, 10, 2);

18. init_plugin_suite_live_search_auto_insert_enabled

Filter to enable or disable auto-inserting the related posts shortcode into post content or comment areas — based on location and post type.

  • Parameters: (bool $enabled, string $position, string $post_type)
  • Default: true if all conditions are met (singular post, allowed post type, correct insertion position)
add_filter('init_plugin_suite_live_search_auto_insert_enabled', function($enabled, $position, $post_type) {
    // Disable auto-insert for "page" post type
    return $post_type === 'page' ? false : $enabled;
}, 10, 3);

19. init_plugin_suite_live_search_default_related_shortcode

Filter to customize the shortcode that gets auto-inserted when related posts are enabled (after content, before/after comments).

  • Parameters: (string $shortcode)
  • Default: [init_live_search_related_posts count="10"]
add_filter('init_plugin_suite_live_search_default_related_shortcode', function($shortcode) {
    return '[init_live_search_related_posts count="5" template="grid"]';
});

20. init_plugin_suite_live_search_ai_candidates

Customize or replace the candidate pool of posts for the AI-powered Related Posts system.

  • Params: (array $candidates, int $post_id, string $post_type)
  • Default: Pulled from multiple sources (recent posts, same series, same same_keyword if available).
add_filter('init_plugin_suite_live_search_ai_candidates', function($candidates, $post_id, $post_type) {
    // Add sticky posts into the candidate pool
    $sticky = get_option('sticky_posts');
    return array_unique(array_merge($candidates, $sticky));
}, 10, 3);

21. init_plugin_suite_live_search_ai_signals

Add or override signals used in AI scoring (tags, series, same_keyword, etc.).

  • Params: (array $signals, int $post_id, int $candidate_id)
add_filter('init_plugin_suite_live_search_ai_signals', function($signals, $post_id, $candidate_id) {
    // Boost score if both posts share the same author
    if (get_post_field('post_author', $post_id) === get_post_field('post_author', $candidate_id)) {
        $signals['author'] = 1;
    }
    return $signals;
}, 10, 3);

22. init_plugin_suite_live_search_ai_weights

Adjust the default weight configuration of signals in AI ranking.

  • Params: (array $weights)
  • Default: tag=0.25, series=0.20, title_bigrams=0.20, same_keyword=0.15, category=0.08, views=0.07, comment=0.05, freshness=0.05
add_filter('init_plugin_suite_live_search_ai_weights', function($weights) {
    // Increase the impact of "same_keyword"
    $weights['same_keyword'] = 0.25;
    return $weights;
});

23. init_plugin_suite_live_search_ai_score

Customize the final computed score of each candidate after signals and weights are applied.

  • Params: (float $score, int $post_id, int $candidate_id, array $signals)
add_filter('init_plugin_suite_live_search_ai_score', function($score, $post_id, $candidate_id, $signals) {
    // Slightly boost if candidate has more than 1000 views
    $views = (int) get_post_meta($candidate_id, '_init_view_count', true);
    if ($views > 1000) $score *= 1.1;
    return $score;
}, 10, 4);

24. init_plugin_suite_live_search_ai_half_life_recency

Customize the half-life (in days) for the recency signal — how “new” a candidate is relative to now.

  • Params: (int $days)
  • Default: 60 days.
add_filter('init_plugin_suite_live_search_ai_half_life_recency', function($days) {
    // Technical blogs: content cools down faster
    return 45;
});

25. init_plugin_suite_live_search_ai_half_life_gap

Customize the half-life (in days) for the time_gap signal — date proximity to the source post.

  • Params: (int $days)
  • Default: 90 days.
add_filter('init_plugin_suite_live_search_ai_half_life_gap', function($days) {
    // Reduce the importance of time alignment
    return 75;
});

26. init_plugin_suite_live_search_ai_mmr_lambda

Adjust the λ parameter for MMR (Max Marginal Relevance) — tradeoff between relevance and diversity.

  • Params: (float $lambda)
  • Default: 0.75 (closer to 1.0 ⇒ more relevance, closer to 0.0 ⇒ more diversity).
add_filter('init_plugin_suite_live_search_ai_mmr_lambda', function($lambda) {
    // Slightly stronger diversity
    return 0.68;
});

27. init_plugin_suite_live_search_ai_selected

Intercept the final ID list after MMR diversification (add/remove/reorder).

  • Params: (array $selected_ids, array $scored_candidates, int $post_id)
add_filter('init_plugin_suite_live_search_ai_selected', function($selected, $scored, $post_id) {
    // Ensure the highest-score candidate is placed first
    if (!empty($scored)) {
        arsort($scored);
        $top = array_key_first($scored);
        $selected = array_values(array_unique(array_merge([$top], $selected)));
    }
    return $selected;
}, 10, 3);

28. init_plugin_suite_live_search_ai_cache_ttl

Customize the cache duration (in seconds) for AI-powered related posts. Return 0 to disable caching.

  • Params: (int $ttl, int $post_id, string $post_type, int $limit)
add_filter('init_plugin_suite_live_search_ai_cache_ttl', function($ttl, $post_id, $post_type, $limit) {
    // Example: set cache to 12 hours for products, keep default for others
    if ($post_type === 'product') {
        return 12 * HOUR_IN_SECONDS;
    }
    return $ttl;
}, 10, 4);

29. init_plugin_suite_live_search_post_types

Allows themes or plugins to customize the list of post_type used for search queries.
Useful if you want to extend or enforce a specific custom post type without affecting the plugin’s settings.

  • Parameters: (array $post_types, array $options, array $args)
  • Default: Based on plugin settings, or fallback to ['post'].
add_filter('init_plugin_suite_live_search_post_types', function($types, $options, $args) {
    // Always include the "manga" post type
    $types[] = 'manga';
    return array_values(array_unique($types));
}, 10, 3);

Conclusion

The powerful filter system makes Init Live Search highly extensible and integrable into any theme or custom plugin. From search logic to UI enhancements, you can customize everything without touching the core plugin code.

Comments


  • No comments yet.

Web-Based Tools

Press Ctrl + \ on desktop, or swipe left anywhere on mobile.

Login