I have a custom endpoint created following the official guide
and in it, I’m simply fetching the post by slug (name) as follows:
if ( $posts = get_posts( array(
'name' => $slug,
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => 1
) ) ) $post = $posts[0];
if ( empty( $post ) ) {
return null;
}
Preparing the response as adding more data about the author, preparing some custom fields etc.
The problem I’m facing is we have Yoast SEO plugin added and in the standard WP REST API yoast_head is included but in the custom endpoint, it’s not.
I found this article where it lists that the SEO data is included in the standard REST API and that there is a custom endpoint for fetching the SEO data
Example:
https://example/wp-json/yoast/v1/get_head?url=https://example.com/hello-world/
That would mean I would be required to make an additional request just to fetch the SEO data.
I also tried activating WP REST Yoast Meta plugin without success as it throws an error while fetching the regular WP REST and it had no effect on the custom one
Is there a way to get yoast_head in the custom endpoint?
2
Answers
Here is what you need, I faced the same issue and someone coded this for this exact purpose:
https://wordpress.org/plugins/wp-rest-yoast-meta/
Keep me posted!
i had the same problem and this worked for me, i hope it helps you. The Meta_Surface class provides a helper for access the head of any post or page.