Relevanssi - A Better Search
[resolved] Weight for attachments (7 posts)

  1. Piani
    Posted 2 years ago #

    Is there a way that I can set "weight" for attachment search. Now they are showing up first before content posts. I would like attachment hits to show last instead.


  2. Mikko Saari
    Plugin Author

    Posted 2 years ago #

    You can create a sorting filter on relevanssi_hits_filter that puts the attachments last. Something like this:

    add_filter('relevanssi_hits_filter', 'rlv_attachments_last');
    function rlv_attachments_last($hits) {
        $attachments = array();
        $everything_else = array();
        foreach ($hits[0] as $hit) {
            if ($hit->post_type == 'attachment') {
                $attachments[] = $hit;
            else {
                $everything_else[] = $hit;
        $hits[0] = array_merge($everything_else, $attachments);
        return $hits;

    Put this in your theme's functions.php.

  3. Piani
    Posted 2 years ago #

    Thanks alot for the code. This really worked excellent!

    Is there a possibility to add in this function filtering away some attachments for example from one special "Author" (of attachment)?

    What Im after is all attachments uploaded by the Author "Webbmaster" is for the design of the site (like background, header, system) that I dont want to display for the public. But all other attachments uploaded by the users is perfect to be public and searchable.

    Or (how) is this made in the plugin featured "Exclusions and restrictions"?

  4. Piani
    Posted 2 years ago #

    I have a lead on this tweaking.

    I use "Exclude these posts/pages from search:". There I can enter the shortcode for the media I dont want to display and that is working fine. Problem is that there are lots of posts in the media library added by the webbmaster that I dont want to index and new files can be added that will be difficult to track and update.

    Can this snippet be used to someting like:
    "Exclude all posts posted by author:" [Webbmaster]

    Can this be done custom or maby a request for development? Then the index can be tweaked to hide the content added for the site building and just display media connected to the posts and pages made by editors?

  5. Piani
    Posted 2 years ago #

    Or can the following be excluded direct for hiding in the above sorting filter?

    post_type = attachment AND post_author = 1

  6. Mikko Saari
    Plugin Author

    Posted 2 years ago #

    If you want to prevent Relevanssi indexing anything from one particular author, the best approach is this:

    add_filter('relevanssi_do_not_index', 'rlv_no_webbmaster');
    function rlv_no_webbmaster($dont_index, $post_id) {
        $post = get_post($post_id);
        if ($post->post_author == 1) $dont_index = true;
        return $dont_index;

    Add this to your functions.php and re-index. That will remove all posts (attachments and everything else) from user 1 from the index. If you want only attachments removed, then

    if ($post->post_author == 1 && $post->post_type == 'attachment') $dont_index = true;

    is correct.

  7. Piani
    Posted 2 years ago #

    Thanks again for a fast reply and excelent easy code to use!

    My tweaks are completley solved!

Topic Closed

This topic has been closed to new replies.

About this Plugin

  • Relevanssi - A Better Search
  • Frequently Asked Questions
  • Support Threads
  • Reviews

About this Topic