apply_filters ( 'jetpack_relatedposts_filter_hits', array $hits, string $post_id )

Filter the Related Posts matched by Elasticsearch.

Source file: modules/related-posts/jetpack-related-posts.php

View in GitHub



(array) Array of Post IDs matched by Elasticsearch.


(string) Post ID of the post for which we are retrieving Related Posts.


Since: Jetpack 2.9.0


This filter allows you to replace one of the Related Posts by a custom result. In the following example we’ll edit the related posts returned on the 2194 post ID. We’ll append post ID 1036 as the first related post result:

function jetpackme_append_related_post( $hits, $post_id ) {
    // $post_id is the post we are currently getting related posts for
        if ( 2194 == $post_id ) {
            // Add 1036 to the front of the hits array
        array_unshift( $hits, array( 'id' => 1036 ) );
            // Remove the last element of the array
            array_pop( $hits );
    return $hits;
add_filter( 'jetpack_relatedposts_filter_hits', 'jetpackme_append_related_post', 20, 2 );

Have a note to contribute?

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s