Support Home > Appearance > Customize Search

Customize Search

Like the other Jetpack modules, the Search module includes filters allowing you to customize how Search works and is displayed.

The code snippets below provide examples of some of the filters included in the module. You can add these code snippets to a functionality plugin, or to your theme’s functions.php file.

You can also check Jetpack’s source code to discover more filters.

Please note that these snippets are provided as a convenience and our support team does not offer assistance on customizing them further.

In order for these filters to appear, you must add the “Search Filters (Jetpack)” widget to your sidebar or other widget area.

Sidebar Filters: Add using the customizer

Search filters can be enabled through the widgets settings or in the customizer. First add the Search (Jetpack) widget to your sidebar, then run a search so that you will be able to customize the results. The filters are only displayed when on a search results page, but the search box (if enabled) in the widget will get displayed on all pages. Also, the filters are only displayed if the current search results would have more than one filter.

Below is an example configuration where we have three types of filters: categories, the month the post was published, and what type of post (eg page/post/product).

Screen Shot 2017-12-29 at 2.13.37 PM.png

Search Widget Settings

Screen Shot 2017-12-29 at 2.11.32 PM

Searching for “post” on a test site.

Sidebar Filters: Add using code

You can also enable sidebar filters on your search results page by adding the Search widget and then customizing them with code. Here is a simple example:

function jp_search_setup_filters() {
    if ( class_exists( 'Jetpack_Search' ) ) {
        Jetpack_Search::instance()->set_filters(array(
            'Categories' => array(
                'type'     => 'taxonomy',
                'taxonomy' => 'category',
                'count'    => 10,
            ),
            'Tags' => array(
                'type'     => 'taxonomy',
                'taxonomy' => 'post_tag',
                'count'    => 10,
            ),
            'Month' => array(
                'type'     => 'date_histogram',
                'field'    => 'post_date',
                'interval' => 'month',
                'count'    => 10,
            ),
        ) );
    } else {
        error_log("Jetpack search does not exist");
    }
}

add_action( 'init', 'jp_search_setup_filters' );

WooCommerce Filters: add product filters to the sidebar

For filtered search of WooCommerce products we can do something similar to the other coding example:

function woo_search_setup_filters() {
    if ( class_exists( 'Jetpack_Search' ) ) {
        Jetpack_Search::instance()->set_filters(array(
            'Categories' => array(
                'type'     => 'taxonomy',
                'taxonomy' => 'product_cat',
                'count'    => 10,
            ),
        ) );
    } else {
        error_log("Jetpack search does not exist");
    }
}

add_action( 'init', 'woo_search_setup_filters' );

Search Filters in action on a WooCommerce site.

Adding filters to the page without using a widget

If you want to add a Filters section to your theme, you can use the Jetpack_Search_Template_Tags::render_available_filters() template tag in the search.php theme template like this:

</pre>
<h2>Filter posts</h2>
<pre>

Note that for any filters to appear, you need to have programmatically defined custom filters as per the examples above.

Customizing the WordPress Search Query

Jetpack Search will only work with the main search WP_Query. If you want to modify the default WordPress search page, for example, including additional post types in search results, we don’t recommend creating a custom WP_Query as this will not work with Jetpack Search. Instead, we recommend using the pre_get_posts filter.

Debug Search Query

Jetpack Search has built in support for two plugins for examining the search query and search query results: Query Monitor and Debug Bar.

Once the plugin is enabled, on any search page you can open the tool and go to the Jetpack Search tab to see the queries that were run and their results.

Search Algorithm Filter: exclude posts by tags/post_id/etc

The posts that are returned as a part of the search can be easily filtered by wrapping the main search query in a bool query that adds some filters.

Here is a simple case that excludes any post with the tag slug exclude_me:

function filter_jetpack_search_query( $es_query_args, $query ) {
    $es_query_args['query'] = array( 'bool' => array(
        'must' => array( $es_query_args['query'] ),
        'must_not' => array(
            array( 'term' => array( 'tag.slug' => 'exclude_me' ) )
        ),
    ) );
    return $es_query_args;
}
add_filter( 'jetpack_search_es_query_args', 'filter_jetpack_search_query', 10, 2 );

Filtering can be made significantly more complex. Here we exclude: post_ids 3, 4, and 5; the category exclude_me; and the tag exclude_me

function filter_jetpack_search_query( $es_query_args, $query ) {
    $es_query_args['query'] = array( 'bool' => array(
        'must' => array( $es_query_args['query'] ),
        'must_not' => array(
            array( 'terms' => array( 'post_id' => array( 3, 4, 5 ) ) ),
            array( 'term' => array( 'category.slug' => 'exclude_me' ) ),
            array( 'term' => array( 'tag.slug' => 'exclude_me' ) ),
        ),
    ) );
    return $es_query_args;
}
add_filter( 'jetpack_search_es_query_args', 'filter_jetpack_search_query', 10, 2 );

Search Algorithm Filter: only search within a category

Sometimes you may only want your search results to be within a single category. Similar to excluding posts, we wrap the main query in a bool query. In this case we will only search for posts that have the category include_me1 or include_me2.

function filter_jetpack_search_query( $es_query_args, $query ) {
    $es_query_args['query'] = array( 'bool' => array(
        'must' => array( $es_query_args['query'] ),
        'filter' => array(
            array( 'terms' => array( 'category.slug' => array( 'include_me1', 'include_me2' ) ) )
        ),
    ) );
    return $es_query_args;
}
add_filter( 'jetpack_search_es_query_args', 'filter_jetpack_search_query', 10, 2 );

Search Algorithm: adjust boosting of recent content

By default the search algorithm slightly boosts recent content over older content. Sometimes this boosting will be too much and you’ll want to completely remove it:

function exclude_jetpack_search_recency( $params ) {
		return false;
}
add_filter( 'jetpack_search_recency_score_decay', 'exclude_jetpack_search_recency' );

Or you may want to make the recency boosting more aggressive for all content that is older than 30 days:

function adjust_jetpack_search_recency( $params ) {
    return array(
        'origin' => date('Y-m-d' ),
        'offset' => '30d', // all posts from the past month treated equally
        'scale'  => '90d', // a post 120 days ago will have the score reduced by half
        'decay' => 0.5,
    );
}
add_filter( 'jetpack_search_recency_score_decay', 'adjust_jetpack_search_recency' );

Currently the search index does not have any view counts available, but you can boost based on the number of comments which often works well:

function boost_jetpack_search_query( $es_query_args, $query ) {
    $es_query_args['query'] = array( 'function_score' => array(
        'query' => $es_query_args['query'],
        'functions' => array(
            array(
                'field_value_factor' => array(
                    'field'    => 'comment_count',
                    'factor'   => 5,
                    'modifier' => 'log2p',
                    'missing'  => 1,
                ),
            ),
        )
    ) );
    return $es_query_args;
}
add_filter( 'jetpack_search_es_query_args', 'boost_jetpack_search_query', 10, 2 );

If you have Jetpack Likes enabled, then you could also boost with a combination of both likes and comments:

function boost_jetpack_search_query( $es_query_args, $query ) {
    $es_query_args['query'] = array( 'function_score' => array(
        'query' => $es_query_args['query'],
        'functions' => array(
            array(
                'field_value_factor' => array(
                    'field'    => 'comment_count',
                    'factor'   => 5,
                    'modifier' => 'log2p',
                    'missing'  => 1,
                ),
            ),
            array(
                'field_value_factor' => array(
                    'field'    => 'like_count',
                    'factor'   => 5,
                    'modifier' => 'log2p',
                    'missing'  => 1,
                ),
            ),
        )
    ) );
    return $es_query_args;
}
add_filter( 'jetpack_search_es_query_args', 'boost_jetpack_search_query', 10, 2 );

Search Algorithm: boost posts with images

For some sites, the best posts are the ones with the most images or galleries in them. We maintain two fields with a count of the number of images and galleries that can be used for boosting on.

function boost_jetpack_search_query( $es_query_args, $query ) {
    $es_query_args['query'] = array( 'function_score' => array(
        'query' => $es_query_args['query'],
        'functions' => array(
            array(
                'field_value_factor' => array(
                    'field'    => 'has.image',
                    'factor'   => 5,
                    'modifier' => 'log2p',
                    'missing'  => 1,
                ),
            ),
            array(
                'field_value_factor' => array(
                    'field'    => 'has.gallery',
                    'factor'   => 5,
                    'modifier' => 'log2p',
                    'missing'  => 1,
                ),
            ),
        )
    ) );
    return $es_query_args;
}
add_filter( 'jetpack_search_es_query_args', 'boost_jetpack_search_query', 10, 2 );

Search Algorithm: boost pages over posts

Matching any filter can be used as a way to boost results. Fields such as post_type and post_format are often good choices for boosting certain types of content over others:

function boost_jetpack_search_query( $es_query_args, $query ) {
    $es_query_args['query'] = array( 'function_score' => array(
        'query' => $es_query_args['query'],
        'functions' => array(
            array(
                'filter' => array(
                    'term' => array( 'post_type' => 'page' )
                ),
                'weight' => 10,
            ),
        )
    ) );
    return $es_query_args;
}
add_filter( 'jetpack_search_es_query_args', 'boost_jetpack_search_query', 10, 2 );

Elasticsearch is a trademark of Elasticsearch BV, registered in the U.S. and in other countries.

  • Table Of Contents

  • Categories

  • Contact Us

    Need more help? Feel free to contact us.