How to use query parameters, as "_fields", to filter data inside an array in the REST API?

admin2025-01-08  4

How to handle this endpoint

/wp-json/wc/v3/products/?_fields=name,meta_data

[
    {
        "name": "Test",
        "meta_data": [
            {
                "id": 478626,
                "key": "_wp_page_template",
                "value": "default"
            },

to achieve this?

[
    {
        "name": "Test",
        "meta_data": [
            {
                "value": "default"
            },

I took a look at the documentation, but I didn't find anything relevant.

Since meta_data is an array, this syntax DOESN'T work:

/wp-json/wc/v3/products/?_fields=name,meta_data.value

How to handle this endpoint

/wp-json/wc/v3/products/?_fields=name,meta_data

[
    {
        "name": "Test",
        "meta_data": [
            {
                "id": 478626,
                "key": "_wp_page_template",
                "value": "default"
            },

to achieve this?

[
    {
        "name": "Test",
        "meta_data": [
            {
                "value": "default"
            },

I took a look at the documentation, but I didn't find anything relevant.

Since meta_data is an array, this syntax DOESN'T work:

/wp-json/wc/v3/products/?_fields=name,meta_data.value

Share Improve this question edited May 27, 2022 at 11:08 Marcel asked May 27, 2022 at 1:26 MarcelMarcel 1033 bronze badges 4
  • 2 The linked documentation says, "As of WordPress 5.3 the _fields parameter supports nested properties", so try _fields=name,meta_data.value..? But can't you just retrieve only the value property from within your application? – Sally CJ Commented May 27, 2022 at 4:25
  • It DOESN'T WORK, Sally, because "meta_data" is an array. – Marcel Commented May 27, 2022 at 10:56
  • Ah yes, sorry I didn't notice the [. And I don't think an "any (numeric) array key" is supported (i.e. the key must be explicitly specified, e.g. meta_data.0.value and meta_data.1.value), so maybe you should just use the standard way - keep the original structure and in your app, get just the property you need from each item in the meta_data array. – Sally CJ Commented May 27, 2022 at 16:43
  • meta_data.0.value DOESN'T work as well. – Marcel Commented May 27, 2022 at 21:15
Add a comment  | 

1 Answer 1

Reset to default 0

Does this only work with the meta_data object for accessing nested items in an array? I tried it with woo commerce subscription line_items and did not seem to work.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736269197a1333.html

最新回复(0)