• 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.)

    Here’s my register_rest_route:

    add_action ('rest_api_init', 'somRegisterSearch');
    
        function somRegisterSearch() {
            register_rest_route('customapi/v1', 'search', array(
                'methods' => WP_REST_SERVER::READABLE,
                'callback' => 'somSearchResults'
            ));
        }
    
        function somSearchResults($data) {
    
            $places = new WP_Query(array(
                'post_type' => 'post',
                's' => sanitize_text_field($data['term']),
    
            ));
            $placesResults = array();
    
            while ($places->have_posts()) {
                $places->the_post();
                
                array_push($placesResults, array(
                    'title' => get_the_title(),
                    'permalink' => get_the_permalink(),
                    'thumbnail' => get_the_post_thumbnail_url(null, 'thumbnail'),
                    'content' => get_the_content(),
                    // 'terms' => get_terms( array(
                    //     'taxonomy' => 'post_tag',
                    //     'hide_empty' => false,
                    // ) )
                ));
            }
    
            return $placesResults;
        }
    
Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator bcworkz

    (@bcworkz)

    You’re using get_the_content() it doesn’t apply “the_content” filter, the_content() does. And inappropriately echoes content ?? Define your own content callback that gets the content, then applies “the_content” filter. You can use the source code for the_content(), changing the name and using return instead of echo.

    Thread Starter david.bruggink

    (@davidbruggink)

    Thanks for the reply; Sorry, I’m a bit lost:
    – I understand that get_the_content() is not doing any filtering in the REST route’s array_push; that can be ignored
    – In my custom the_content filter at the top, I am using return and not echo to get the modified $newContent. I’m not sure how this is different from defining my own content callback

    Moderator bcworkz

    (@bcworkz)

    Err, my bad. Forget the part about defining your own function. In your existing callback just do this:
    'content' => apply_filters('the_content', get_the_content()),

    In the_content() source, it also does a HTML entity conversion on ‘]]>’. Unless you are likely to have such a string, that part can be ignored.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Filter on the_content doesn’t update the content in REST API results’ is closed to new replies.