Skip to main contentSkip to footer

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.

Your account

Not logged in. Log in to see your license details.

Search

Popular Resources

Menu problems in multisite search
Sometimes multisite searches cause problems in navigation menus. This is caused by pages from another subsite going into the page…
PostX Pro
If you use PostX Pro to build a search results template using their Post Grid block, you’ll notice that the…
Search results in random order
Do you want to order your search results in random order? Here’s how: add_filter( 'relevanssi_hits_filter', 'shuffle_search' ); function shuffle_search( $hits…

Related Posts:

Comment Section:

Leave a Reply

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

Fill out this field
Fill out this field
Please enter a valid email address.
You need to agree with the terms to proceed