• Resolved TheGreyRabbit

    (@ayo-1)


    Hi,

    Is there a way to exclude .pdf files from search results?

    I’m currently getting a lot of .pdf results that I don’t wish on the search result page.

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

    (@msaari)

    If you don’t ever want to see them, it’s best not to index them in the first place. The PDF files are attachments. Do you need any attachments in the results? If the answer is no, uncheck the attachment post type in the Relevanssi indexing settings and rebuild the index.

    If you want to include some other form of attachments, you need a filter function that excludes the PDFs based on the file type. See here for instructions on that.

    Thread Starter TheGreyRabbit

    (@ayo-1)

    Hi Mikko,

    thanks for your reply! I actually do need the attachments indexed otherwise I’m not getting correct results for search queries where the query is only present in the name of the image of a product variation.

    But as I’m having a lot of pdf files with technical data, I don’t wish those to appear in search results. I checked the page you provided, but I’m not getting how to exclude only pdfs from indexing and SRP.

    Plugin Author Mikko Saari

    (@msaari)

    Add this to your theme functions.php and rebuild the index:

    add_filter( 'relevanssi_indexing_restriction', 'rlv_no_pdf_attachments' );
    function rlv_no_pdf_attachments( $restriction ) {
        global $wpdb;
        $restriction['mysql']  .= " AND post.ID NOT IN (SELECT ID FROM $wpdb->posts WHERE post_type = 'attachment' AND post_mime_type = 'application/pdf' ) ";
        $restriction['reason'] .= ' No PDFs';
        return $restriction;
    }
    Thread Starter TheGreyRabbit

    (@ayo-1)

    After adding it like this https://prnt.sc/kSFjGDdaeyTU , there’s fatal error on the site.

    Thread Starter TheGreyRabbit

    (@ayo-1)

    It was a bad copy of the snippet, sorry. I’m now rebuilding the index.

    Thank you!

    Thread Starter TheGreyRabbit

    (@ayo-1)

    Hey Mikko,

    it works! Thank you!

    Is there a way the above code can be modified in order to exclude pdf and all unattached images or I have to copy the above code and add the full code for excluding unattached attachments from your guide?

    Plugin Author Mikko Saari

    (@msaari)

    You can combine both restrictions in one function:

    add_filter( 'relevanssi_indexing_restriction', 'rlv_no_pdf_attachments' );
    function rlv_no_pdf_attachments( $restriction ) {
        global $wpdb;
        $restriction['mysql']  .= " AND post.ID NOT IN (SELECT ID FROM $wpdb->posts WHERE post_type = 'attachment' AND (post_mime_type = 'application/pdf' OR post_parent = 0) ) ";
        $restriction['reason'] .= ' No PDFs';
        return $restriction;
    }
    Thread Starter TheGreyRabbit

    (@ayo-1)

    Thanks, Mikko! I appreciate your support!

    Thread Starter TheGreyRabbit

    (@ayo-1)

    Mikko, I will be very thankful if you can help with one last tweak of the code above. How should the snippet look if I would like to exclude from results all pdf + unattached attachments + all attachments that contain the following word in Cyrillic: “схема” ?

    Thanks for your help!

    Plugin Author Mikko Saari

    (@msaari)

    Contain the word where? In the title, in the Relevanssi-read content or where?

    Thread Starter TheGreyRabbit

    (@ayo-1)

    In the title

    Plugin Author Mikko Saari

    (@msaari)

    add_filter( 'relevanssi_indexing_restriction', 'rlv_no_pdf_attachments' );
    function rlv_no_pdf_attachments( $restriction ) {
        global $wpdb;
        $restriction['mysql'] .= " AND post.ID NOT IN (SELECT ID FROM $wpdb->posts WHERE post_type = 'attachment' AND (post_mime_type = 'application/pdf' OR post_parent = 0 OR post_title LIKE '%схема%') ) "; $restriction['reason'] .= ' No PDFs';
        return $restriction;
    }

    This should do it.

    • This reply was modified 10 months, 3 weeks ago by Mikko Saari.
    Thread Starter TheGreyRabbit

    (@ayo-1)

    Is there a chance to have a typo in the last provided snippet?

    Plugin Author Mikko Saari

    (@msaari)

    I checked and didn’t notice one.

    Thread Starter TheGreyRabbit

    (@ayo-1)

    Thanks, Mikko! Greatly appreciated!

Viewing 15 replies - 1 through 15 (of 15 total)
  • The topic ‘Exclude .pdf from search results’ is closed to new replies.