Posted on

Pin a post for all searches

Relevanssi Premium has tools for pinning posts for specific queries and all searches where the post appears, but what if you want to pin a post for all searches? That’s also possible but requires a bit of code.

Pin the post for all searches and add this function to your site:

add_filter( 'relevanssi_hits_filter', function( $hits ) {
    $pinned_post_id = 1234;
    if ( ! in_array( $pinned_post_id, array_column( $hits[0], 'ID' ), false ) ) {
        $pinned_post = get_post( $pinned_post_id );
        array_unshift( $hits[0], $pinned_post );
    }
    return $hits;
}, 11 );

Replace 1234 with the ID of the post you want to pin. This function runs on the relevanssi_hits_filter hook that can be used to modify the search results. The function checks if the post appears in the results; if it doesn’t, it fetches the post object and adds it to the top.

Leave a Reply

Are you a Relevanssi Premium customer looking for support? Please use the Premium support form.

Your email address will not be published. Required fields are marked *