Support » Plugins and Hacks » Relevanssi - A Better Search » [Resolved] Weight for attachments

[Resolved] Weight for attachments

Viewing 6 replies - 1 through 6 (of 6 total)
  • Plugin Author Mikko Saari


    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.



    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”?



    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?



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

    post_type = attachment AND post_author = 1

    Plugin Author Mikko Saari


    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.



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

    My tweaks are completley solved!

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘[Resolved] Weight for attachments’ is closed to new replies.
Skip to toolbar