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

WooCommerce 4.4 problems
Updating WooCommerce to version 4.4 breaks Relevanssi searches. After updating WooCommerce to version 4.4, Relevanssi searches no longer find results.…
Showing only one recurring event
Some event calendar plugins do recurring events by creating many posts. That’s fine until those cloned posts fill up your…
Modifying the search form on the fly

I set up a short coded search box to search my Learndash e-course for one specific category only: [ searchform ld_topic_category=”156″ ] It works perfectly except one thing: if no results show, it takes you to the default WordPress page and presents you with the standard search box that no…

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