Custom Taxonomy in REST API

admin2025-01-07  9

I have two custom taxonomies that I use for my posts.

I am trying to retrieve the names from only one of them.

In REST API V2, I do the following:

/?_embed=wp:term

This embeds the actual names for ALL taxonomies (Categories plus my two custom ones) but I am only interested in the names associated with each post only for one of the custom taxonomies.

Example response:

"_embedded": {
    "wp:term": [
        [
            {
                "id": 2272,
                "link": "/",
                "name": "Cat1",
                "slug": "cat1",
                "taxonomy": "category",
                "_links": {
                ....
                }
            }
        ],
        [],
        [
            {
                "id": 1740,
                "link": "/",
                "name": "Custom Name",
                "slug": "custom-name",
                "taxonomy": "custom-tax-1",
                "_links": {
                ....
                }
            }
        ],
        [
            {
                "id": 2148,
                "link": "/",
                "name": "Custom Name 1",
                "slug": "custom-name-1",
                "taxonomy": "custom-tax-2",
                "_links": {
                ....
                }
            },
            {
                "id": 2150,
                "link": "/",
                "name": "Custom Name 2",
                "slug": "custom-name-2",
                "taxonomy": "custom-tax-2",
                "_links": {
                ....
                }
            }
        ]
    ]
}

So I get back these taxonomies:

  1. category (1 item)
  2. custom-tax-1 (1 item)
  3. custom-tax-2 (2 items)

How can I make it so it only returns values for custom-tax-2?

I have two custom taxonomies that I use for my posts.

I am trying to retrieve the names from only one of them.

In REST API V2, I do the following:

https://example.com/wp-json/wp/v2/posts/14340/?_embed=wp:term

This embeds the actual names for ALL taxonomies (Categories plus my two custom ones) but I am only interested in the names associated with each post only for one of the custom taxonomies.

Example response:

"_embedded": {
    "wp:term": [
        [
            {
                "id": 2272,
                "link": "https://example.com/category/cat1/",
                "name": "Cat1",
                "slug": "cat1",
                "taxonomy": "category",
                "_links": {
                ....
                }
            }
        ],
        [],
        [
            {
                "id": 1740,
                "link": "https://example.com/custom1/custom-name/",
                "name": "Custom Name",
                "slug": "custom-name",
                "taxonomy": "custom-tax-1",
                "_links": {
                ....
                }
            }
        ],
        [
            {
                "id": 2148,
                "link": "https://example.com/custom2/custom-name-1/",
                "name": "Custom Name 1",
                "slug": "custom-name-1",
                "taxonomy": "custom-tax-2",
                "_links": {
                ....
                }
            },
            {
                "id": 2150,
                "link": "https://example.com/custom2/custom-name-2/",
                "name": "Custom Name 2",
                "slug": "custom-name-2",
                "taxonomy": "custom-tax-2",
                "_links": {
                ....
                }
            }
        ]
    ]
}

So I get back these taxonomies:

  1. category (1 item)
  2. custom-tax-1 (1 item)
  3. custom-tax-2 (2 items)

How can I make it so it only returns values for custom-tax-2?

Share Improve this question asked Apr 14, 2021 at 23:50 user3542212user3542212 714 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

Figured it out. Just needed to make a call like this to get the values of the custom taxonomy for a specific post:

https://example.com/wp-json/wp/v2/custom-tax-2?post={id}
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736254308a194.html

最新回复(0)