wp rest api

This page is part of the plugin documentation for the Related Posts by Taxonomy plugin.

This plugin allows you to make use of the WordPress REST API to get related posts. This is an opt-in feature that has to be activated with a filter.

Note: This feature is only available for WordPress versions 4.7 and up


Use this in your (child) theme’s functions.php file to activate the WordPress REST API support for this plugin..

add_filter( 'related_posts_by_taxonomy_wp_rest_api', '__return_true' );

This will provide you with this endpoint where you can use any (valid) post id to get related posts.


Query Variables

You can use all attributes used by the shortcode as query variables;

Example to get 10 related posts for a post with id 1010.


Extra query vars for the WordPress REST API
There is one extra query var for the rest api.

(string) Set the fields that should be returned for related posts. By default all post fields are returned. Use this query var to only return ids or titles or slugs. Accepts ids, names, slugs. Default none.

Rest Response

The WordPress REST API will return a json object with the following properties

  • posts – The related posts
  • termcount – Number of related terms in common
  • post_id – The Post ID used to get related posts for
  • post_types – The Post types used in the query for related posts
  • taxonomies – The taxonomies used in the query for related posts
  • related_terms – The related term ids used in the query for related posts.
  • rendered Rendered related posts HTML

Example query for related post ids for post with id 1010


Example response

   "rendered":"<div class=\"rpbt_wp_rest_api\">\n<h3>Related Posts<\/h3>\n<ul>\n\t\t\t<li><a href=\"http:\/\/test.dev\/2009\/06\/01\/edge-case-many-tags\/\" title=\"Edge Case: Many Tags\">Edge Case: Many Tags<\/a><\/li>\n\t\t\t<li><a href=\"http:\/\/test.dev\/2013\/01\/09\/markup-text-alignment\/\" title=\"Markup: Text Alignment\">Markup: Text Alignment<\/a><\/li>\n\t\t\t<li><a href=\"http:\/\/test.dev\/2013\/01\/11\/markup-html-tags-and-formatting\/\" title=\"Markup: HTML Tags and Formatting\">Markup: HTML Tags and Formatting<\/a><\/li>\n\t\t\t<li><a href=\"http:\/\/test.dev\/2009\/05\/15\/edge-case-nested-and-mixed-lists\/\" title=\"Edge Case: Nested And Mixed Lists\">Edge Case: Nested And Mixed Lists<\/a><\/li>\n\t\t\t<li><a href=\"http:\/\/test.dev\/2013\/01\/05\/title-with-special-characters\/\" title=\"Markup: Title With Special Characters\">Markup: Title With Special Characters<\/a><\/li>\n\t<\/ul>\n<\/div>"