Filter on the_content doesn’t update the content being searched via register_rest_route


I’m trying to get my custom REST route to include search results from a filtered the_content. I’m trying to automatically include the post tag names within each post’s content, so that searching can be performed on the post tags without the need to copy and paste the tags into each post’s content. My content filter successfully returns the tags on each page like I want:

    // Customise the_content
    function wpa_content_filter( $content ) {

        $post_tags = get_the_tags();

        if ( $post_tags ) {
            $tags = get_the_tags();
            $html = '<div class="post_tags">';
            foreach ( $tags as $tag ) {
                $tag_link = get_tag_link( $tag->term_id );

                $html .= "<a href='{$tag_link}' title='{$tag->name} Tag' class='{$tag->slug}'>";
                $html .= "{$tag->name}</a>";
            $html .= '</div>';
            echo $html;
            $newContent = $content.$html;
        } else {
            $newContent = $content;

        return $newContent;
    add_filter( 'the_content', 'wpa_content_filter' );

But my REST API doesn’t return the same filtered content:

"content": "<!-- wp:paragraph -->n<p>Scarborough Marsh is located southwest of Portland, Maine.</p>n<!-- /wp:paragraph -->"

(This should be displaying the tags as well, like it is on the page.)

, , , David Merritt 3 years 2020-08-20T13:10:22-05:00 0 Answers 55 views 0

Leave an answer