• I have been looking through the Relevanssi documentation and previous support tickets to figure out this issue, but all of my attempts have been unsuccessful. I am trying to ignore content that is found in specific custom Gutenberg blocks from being indexed by Relevanssi because our search results are being populated with posts that are irrelevant and only appear because they include the searched term in a few specific blocks that aren’t really part of the post’s pertinent content.

    First I tried using the relevanssi_block_to_render hook to return null if the block is one of the ones we want to ignore:
    function relevanssi_no_index_blocks( $block ) {
    $blocks_to_ignore = array( 'rcp-accordion', 'rcp-faqs', 'rcp-testimonials' );
    if ( in_array( $block['blockName'], $blocks_to_ignore ) ) {
    return null;
    }
    return $block;
    }

    Then I tried using the relevanssi_rendered_block hook to change the content of those same blocks to be an empty string before Relevanssi finishes indexing the content:
    function relevanssi_empty_out_block_content( $content, $block ) {
    $blocks_to_ignore = array( 'rcp-accordion', 'rcp-faqs', 'rcp-testimonials' );
    if ( in_array( $block['blockName'], $blocks_to_ignore ) ) {
    $content = '';
    }
    return $content;
    }

    I also updated the markup of these blocks to add a “relevanssi_noindex” class to the block output and I manually added that class to the blocks in the Gutenberg editor.

    None of these measures fixed the issue. I cleared cache and rebuilt the Relevanssi index each time I tried one of these approaches. What am I doing wrong? How can I accomplish what I’m trying to do here? I still want these posts to be indexed, I just don’t want the text/terms inside of these specific blocks to be indexed with the post because I don’t want posts to be displayed in search results if the only place in the post the search term is found is within these three blocks. Thanks for your help!

    The page I need help with: [log in to see the link]

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

    (@msaari)

    Have you tried debugging these functions to see why they are not working?

    In the first one, check what values $block['blockName'] gets when you save the post. Is the name correct? Usually Gutenberg block names are prefixed with something, so perhaps there’s a prefix you’re missing.

    Thread Starter korynorthrop

    (@korynorthrop)

    @msaari thanks for the quick reply, I already did that to ensure that the blocks were properly detected (I logged the name of the block to the PHP error log in those Relevanssi hooks). The names are correct (I shortened them in this support ticket for brevity’s sake, but an example of the full block name is “acf/rcp-faqs” and that is what gets printed in the error_log when I checked that I had the right names).

    Plugin Author Mikko Saari

    (@msaari)

    So the function triggers and returns null for the blocks? If you check with relevanssi_post_content on a priority higher than 10 (see https://www.relevanssi.com/user-manual/filter-hooks/relevanssi_post_content/), is the rendered block output included or not?

    If you check with the Relevanssi debugger, do the functions have any effect on the Relevanssi indexing?

    Thread Starter korynorthrop

    (@korynorthrop)

    Yes, the relevanssi_block_to_render function is returning null for the desired blocks, which I verified by PHP error_log messages (e.g. right before the return null; I have this error_log( 'Relevanssi: Returning null for block: ' . $block['blockName'] ); and I see this printed out in the PHP log quite a bit as expected).

    Using the relevanssi_post_content hook with a priority of 20, I am outputting the $post_content of one of the posts that shouldn’t be indexed because the only place the search term shows up is in one of the custom blocks that is supposedly returned null by the previous hook. However, when I look at the $post_content that is printed to the error log, I still see the unwanted blocks and the search term content inside those block’s markup.

    Plugin Author Mikko Saari

    (@msaari)

    That’s curious. I don’t know what’s going on here and where the block content comes in. What you’re doing should be enough.

    What if you delete to block content in the relevanssi_post_content function? Does that get you rid of it for good?

    Thread Starter korynorthrop

    (@korynorthrop)

    I was able to get rid of one of these undesired posts from the search results by returning an empty string for it’s $post_content in the relevanssi_post_content filter. So, something is working, now to figure out why I’m not able to get the other filters to work properly….Do you have any other ideas of how to troubleshoot this?

    Plugin Author Mikko Saari

    (@msaari)

    Well, you could always investigate what happens in the relevanssi_gutenberg_block_rendering() function (it’s in /lib/compatibility/gutenberg.php) to see how the block is handled and what the function returns. If the function returns an empty string as it should, then the next step is to figure out where the block content is coming from if not there. Is it possible the content is also somewhere else?

Viewing 7 replies - 1 through 7 (of 7 total)
  • You must be logged in to reply to this topic.