Skip to main contentSkip to footer

Relevanssi lets you index attachments. But perhaps you only want to index a particular type of attachment? Relevanssi settings don’t have any control over that, it’s either all attachments or nothing.

It is possible to choose which kinds of attachments are indexed. It is done with the relevanssi_indexing_restriction filter hook, which lets you control which posts are indexed. You can use the attachment MIME type to see which kind of attachment it is and use that information to weed out unwanted attachments.

No images

To remove all image attachments from the index, add this code to your site and rebuild the index. It will weed out all attachments that have a MIME type that begins with image.

add_filter( 'relevanssi_indexing_restriction', 'rlv_no_image_attachments' );
function rlv_no_image_attachments( $restriction ) {
    global $wpdb;
    $restriction['mysql']  .= " AND post.ID NOT IN (SELECT ID FROM $wpdb->posts WHERE post_type = 'attachment' AND post_mime_type LIKE 'image%' ) ";
    $restriction['reason'] .= ' No images';
    return $restriction;
}

Only PDFs

This function will only index PDF attachments, and nothing else.

add_filter( 'relevanssi_indexing_restriction', 'rlv_only_pdfs' );
function rlv_only_pdfs( $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'] .= 'Not a PDF';
    return $restriction;
}

Only attached attachments

This function will only index attachments that have a parent post.

add_filter( 'relevanssi_indexing_restriction', 'rlv_only_attached' );
function rlv_only_attached( $restriction ) {
    global $wpdb;
    $restriction['mysql']  .= " AND post.ID NOT IN (SELECT ID FROM $wpdb->posts WHERE post_type = 'attachment' AND post_parent = 0 ) ";
    $restriction['reason'] .= 'Not attached';
    return $restriction;
}