Skip to main contentSkip to footer

The Relevanssi attachment indexing assumes the files are connected to the posts using the WordPress attachment mechanism. What if you don’t use that but instead add the files to the pages using the File block in the block editor?

That’s not a problem, but it requires some extra code. Add this function to your site to index the contents of files added with the File block:

add_filter( 'relevanssi_block_to_render', function( $block ) {
    if ( 'core/file' === $block['blockName'] ) {
        $file_id      = $block['attrs']['id'];
        $file_content = get_post_meta( $file_id, '_relevanssi_pdf_content', true );
        if ( $file_content ) {
            $block['innerContent'][0] = $file_content;
        }
    }
    return $block;
} );

This function uses the relevanssi_block_to_render filter hook. Whenever a core/file block is encountered, the function fetches the file contents from the _relevanssi_pdf_content custom field for the attachment post and replaces the block contents with that.

This works great for indexing but does not include the file contents in excerpts. That requires another function:

add_filter( 'relevanssi_pre_excerpt_content', function( $content, $post) {
    $m = preg_match( '/<!-- wp:file.*?"id":(\d+).*?<!-- \/wp:file -->/s', $content, $matches );
    if ( $m > 0 ) {
        $file_id = $matches[1];
        $file_content = get_post_meta( $file_id, '_relevanssi_pdf_content', true );
        if ( $file_content ) {
            $content = preg_replace( '#' . preg_quote( $matches[0], '#' ) . '#', $file_content, $content );
        }
    }
    return $content;
}, 10, 2 );

This filter uses relevanssi_pre_excerpt_content and replaces the file block comment in the post content with the file contents from the custom field.

Your account

Not logged in. Log in to see your license details.

Search

Popular Resources

Custom weighing with relevanssi_match and relevanssi_results

…the name of the field. add_filter( ‘relevanssi_match’, ‘custom_field_weights’ ); function custom_field_weights( $match ) { $custom_field_detail = json_decode( $match->customfield_detail ); if ( null === $custom_field_detail || ! isset( $custom_field_detail->field_name ) ) { $match->weight = 0; } return $match; } This function sets the weight of the match to if Relevanssi doesn’t……= 500 / $diff_days; $weight = $weight * $multiplier; } ); return $results; } Custom field -based weight This function will double the weight of posts with the custom field “featured” set to 1. add_filter( ‘relevanssi_match’, ‘custom_field_weights’ ); function custom_field_weights( $match ) { $featured = get_post_meta( $match->doc, ‘featured’, true );……> ) { $match->weight *= 10; } return $match; } With Relevanssi Premium, you also have the customfield_detail field, which allows you to filter results by custom field. If you want a search that only targets one specific custom field, you can do a filter like this, where field_name is…

WPML: Replacing posts with translations

Here’s a different approach to WPML searching. By default, you have two options. You can include posts in all languages in the results, or you can only use the current language. This solution checks for matches also in other languages. If Relevanssi finds any, this script then replaces them with……isset( $hit->blog_id ) ) { // This is a multisite search. switch_to_blog( $hit->blog_id ); if ( function_exists( ‘icl_object_id’ ) ) { // Reset the WPML cache when blog is switched, otherwise WPML // will be confused. global $wpml_post_translations; $wpml_post_translations->reload(); } } global $sitepress; // Check if WPML is used. if…

FacetWP
Relevanssi is generally well compatible with FacetWP, which provides advanced filtering and faceting capabilities for the search. There are, however,…

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