How to get tag based message list in WP Rest API V2?

I have an application that retrieves data from a Wordpress site using the WP Rest API V2. Recently, the API has changed the use of "filter [tag] =" to a new syntax. I am looking to get a list of posts within a specific tag.

Old syntax:

http://demo.wp-api.org/wp-json/wp/v2/posts?filter[tag]=slug

The new syntax is as follows:

http://demo.wp-api.org/wp-json/wp/v2/posts?tags=id

Now, instead of the slug value, an integer ID is used as an argument. I looked through the documentation, but I can not find a solution for this. I have no way to use id, I can only use slug!

The WP Rest API documentation can be found here: https://developer.wordpress.org/rest-api/reference/posts/

Do you have any idea how to solve this?

+4
source share
2 answers

Just get a list of tags by calling a http://demo.wp-api.org/wp-json/wp/v2/tagsspecific tagId before your request. This way you will find the tag that you need by filtering the data by your "slug". The demo API does not support CORS, so I am not creating a working script.

Nested failed HTTP request example

 $http({
    method: 'GET',
    url: 'http://demo.wp-api.org/wp-json/wp/v2/tags'   
 }).then(function (result) {

      var filtered = filterFilter(result.data, { slug: 'dolor'});

      $http({
        method: 'GET',
        url: 'http://demo.wp-api.org/wp-json/wp/v2/posts?tags=' + filtered[0].id
     }).then(function (result) {
          console.log(result.data);
     });
 });
+1
source

Looking at the official docs API for TAGS , you can search for strings in a tag scheme with this query:

http://demo.wp-api.org/wp-json/wp/v2/tags?search=yourslug

As a result of the JSON output, tags containing the string you were looking for in any of the fields of the scheme, for example, will be displayed slug.

, , , ID ( @lin).

0

Source: https://habr.com/ru/post/1670040/


All Articles