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

MemberPress Downloads add-on

The Downloads add-on for MemberPress adds downloadable files to MemberPress. These files are stored outside the Media Library, so by default Relevanssi attachment indexing has no access to them. However, Relevanssi offers filter hooks you can use to make Relevanssi index the MemberPress Downloads files. Adjust the query First, we……need to tell Relevanssi to look for mpdl-file posts instead of attachment posts using the relevanssi_get_attachment_posts_query filter hook. By default, Relevanssi looks for posts with the post type attachment, post status inherit and a suitable MIME type. These all need to be changed, because the Downloads posts have the post……to modify the name and path of the file sent to Relevanssi: add_filter( ‘relevanssi_get_attached_file’, ‘rlv_mpdl_file’, 10, 2 ); /** * Filters the name and path of the file. * * Gets the file name from the _mpdl_file_filename custom field of the download file * post and corrects the path, too….

The search form shortcode

Relevanssi includes a search form shortcode that can be used to display a search form. The shortcode is searchform and it was introduced in Relevanssi Premium 2.0 and Relevanssi 4.0. Adding query parameters This shortcode prints out a basic search form. If you want to add additional query parameters, that’s……return $form; } Add this code to your site. Note this will then apply to all search forms, not just the ones generated by the shortcode. Versions 2.2.4 (Premium) and 4.1.3 (free) added a new filter hook, relevanssi_search_form, which does the same but only applies to the shortcode search form….…easy: just add parameters to the shortcode. Here are some examples: Search for: will restrict the search form to just products. Search for: will restrict the search form to posts, pages and news. Search for: will restrict the search form to categories 10, 14 and 17. Search for: will do…

WP e-Commerce
WP e-Commerce is a popular shopping cart plugin for WordPress. This code by Alexander Gieg, added to functions.php for example,…

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