Filter on the_content doesn’t update the content in REST API results
-
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; }
- The topic ‘Filter on the_content doesn’t update the content in REST API results’ is closed to new replies.