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

Adding a search form in the navigation menu

…the SearchWP Modal Search Form. As the name says, the plugin is from the SearchWP developer Jon Christopher, but it also works great with Relevanssi. This plugin can easily add a search feature to your navigation menu or a site header. The search form also works great on mobile devices….…header. That’s a good reference if you want to build a nice search form in the header. If your theme does not include a built-in search form and you’re not interested in building one yourself, one of the best plugin options is the SearchWP Modal Search Form. As the name…WordPress doesn’t make adding a search form in the navigation menu easy. It’s a popular request, though, and having access to the search in the site header is typical. Relevanssi itself doesn’t have opinions on the user interface. Some themes, like Twenty Twenty, include a built-in search feature in the…

WooCommerce: Popularity and price sorting

Many WooCommerce users use search sorting that allows users to sort by popularity or price. Unfortunately, while Relevanssi works fine with WooCommerce, those sorts do not work. Relevanssi doesn’t know about price or popularity, and the sorting assumes there’s a default WP search underneath. Relevanssi doesn’t do meta field sorting……as easily as the default WP search does. This function handles price, popularity and rating sorting: add_filter( ‘relevanssi_orderby’, ‘woocommerce_relevanssi_orderby’ ); function woocommerce_relevanssi_orderby( $orderby ) { if ( in_array( $orderby, array( ‘price’, ‘price-desc’ ), true ) ) { global $wp_query; $orderby = ‘meta_value_num’; $wp_query->query_vars[‘meta_key’] = ‘_price’; } if ( ‘price’ ===……$orderby ) { global $rlv_wc_order; $rlv_wc_order = ‘asc’; } if ( ‘date ID’ === $orderby ) { $orderby = ‘post_date’; } if ( ‘popularity’ === $orderby ) { global $wp_query, $rlv_wc_order; $orderby = ‘meta_value_num’; $rlv_wc_order = ‘desc’; $wp_query->query_vars[‘meta_key’] = ‘total_sales’; } return $orderby; } add_filter( ‘relevanssi_order’, ‘woocommerce_relevanssi_order’ ); function woocommerce_relevanssi_order(…

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