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

SearchWP Live Ajax Search

…One example is searching for attachments. By default, SearchWP Live Ajax Search doesn’t include any attachments in the search results. The post type is not a problem, but the post status is: SearchWP Live Ajax Search includes only posts with the post status publish, while attachments have a status of…Relevanssi doesn’t ship with a live search or autocomplete feature that would display results as the user types the search terms. There are many other plugins that provide this functionality, but few of these plugins work with Relevanssi. SearchWP Live Ajax Search is the best one that does. It’s very……to include private posts for the users who are allowed to see them. Just removing the post_status parameter will include drafts in the results. Normally drafts shouldn’t appear in searches, but since the SearchWP Live Ajax Search is an AJAX search, it is done in admin context, where drafts are…

Indexing private custom fields for admins only

I use private custom fields. I want to index them in the administration but not on the public site. How to do ? Initially this seems impossible to do with Relevanssi, but it’s possible. Doing this requires Relevanssi Premium. The free version doesn’t know which custom field matches the search term,…

Boosting shorter posts with higher keyword density

…15 search term appearances might well be a better match for the search than a 2000-word post with 20 search term appearances, as the density is much higher in the shorter post. One way to make Relevanssi give a boost to shorter posts is the add a consideration for the……a boost to shorter posts that have a higher weight and will punish very long posts that rank high just for being long. The version above only considers post content. It does not count the attachment content. This version includes that: add_filter( ‘relevanssi_match’, ‘rlv_inverse_document_length’, 10, 2 ); function rlv_inverse_document_length( $match,…

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