Support » Plugin: Relevanssi - A Better Search » Removing shortcodes from a custom field (not in excerpts)

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


    If Relevanssi doesn’t generate the excerpts, then no Relevanssi filter will do anything, indeed.

    If you’re directly printing out custom fields, don’t use a filter, but instead just pass the custom field contents through the filter.

    If you can elaborate on what you actually are doing here, I can offer better suggestions.

    I am using a page builder (Oxygen) and the content is stored in a custom field ‘ct_builder_shortcodes’.
    So I just add this custom field in the indexing page.
    So far so good, everything works as expected. The search engine can find everything from my articles.

    The only problem is that this custom field also contains lots of shortcodes, so it can make the search not very relevant. That’s why I wanted to try to remove them, with something like this :
    $content = preg_replace(‘/\[.*?\]/g’, ”, $content);

    Now I am not sure if it’s really possible?

    Plugin Author Mikko Saari


    Sure it’s possible. If you want to modify the content that Relevanssi reads from the custom fields, you can use the relevanssi_custom_field_value filter hook – every custom field value that Relevanssi indexes passes through that filter. The filtered value is the field contents, there’s also parameters for the field name and post ID.

    So, something like this:

    add_filter( 'relevanssi_custom_field_value', 'rlv_custom_field_shortcodes' );
    function rlv_custom_field_shortcodes( $content ) {
        $content = preg_replace( '/\[.*?\]/g', '', $content );
        return $content;

    It seems to do the trick.
    I just had to change the /g to /s for the regex

    Thank you very much for your help

    I just wrote a tutorial about how to make Relevanssi works better with Oxygen Builder :

    Thanks again for your help

    Plugin Author Mikko Saari


    Great, thanks!

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Removing shortcodes from a custom field (not in excerpts)’ is closed to new replies.