filter

jetpack_relatedposts_filter_hits

Filter the Related Posts matched by Elasticsearch.

Parameters

$hits
array

Array of Post IDs matched by Elasticsearch.

$post_id
string

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

Changelog

How to use this hook

See “How to use actions and filters to customize Jetpack”.

Notes

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 );