wp rest api

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

Notice: This documentation applies to the development version (2.3.0). This feature will be released soon

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

Activation

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.

/wp-json/related-posts-by-taxonomy/v1/posts/[post_id]

Query Variables

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

Example to get 10 related post ids for post id 1177

/wp-json/related-posts-by-taxonomy/v1/posts/1177?posts_per_page=10

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

fields
(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.
/wp-json/related-posts-by-taxonomy/v1/posts/1177?fields=ids

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 1177

/wp-json/related-posts-by-taxonomy/v1/posts/1177?fields=ids

Example response

{
   "posts":[
      "1151",
      "1176",
      "1178",
      "1000",
      "1174"
   ],
   "termcount":[
      "6",
      "5",
      "4",
      "3",
      "2"
   ],
   "post_id":1177,
   "post_types":[
      "post"
   ],
   "taxonomies":[
      "category",
      "post_tag",
      "post_format"
   ],
   "related_terms":[
      67,
      71,
      79,
      81,
      106,
      116,
      29
   ],
   "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>"
}