Skip Navigation

[Resolved] Relationship in API REST

This support ticket is created 4 years, 1 month ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Our next available supporter will start replying to tickets in about 5.99 hours from now. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Hong_Kong (GMT+08:00)

Tagged: 

This topic contains 11 replies, has 2 voices.

Last updated by avansisI-2 4 years ago.

Assisted by: Luo Yang.

Author
Posts
#1831271

Hi!,

I have a custom type with you relationship, and need that this information is in API rest of custom type.

i'm using a plugin REST API CONTROLLER and works well but not add information in custom type API about relationship,

Do you know any form to do this?

Thanks!

#1831773

Hello,

Toolset relationship isn't a custom field or taxonomy, so it won't be exposed in WP REST api directly, you will need to use custom codes to exposed the related post information with custom codes, for example, you can follow WP document to add a field in the REST api result:
https://developer.wordpress.org/reference/functions/register_rest_field/

And use Toolset function toolset_get_related_post() to get the related parent post information:
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_post

#1831915

Thanks for you reply,

I'm lose with this theme, could you give me an example?

In my case, i have one custom type called 'comercios' and i need that in JSON 'comercios' i can get ID that two custom types related called 'productos' and 'promociones'

If i modified api-rest.php with this

* @var traer relationship promociones y productos
*
*/
$promociones = toolset_get_related_post( $comercio_id, array( 'promociones', 'comercio' ) );
$productos = toolset_get_related_post( $comercio_id, array( 'productos', 'comercio' ) );

doesnt work.

#1832713

For example, there are two post types in your website:
- Page
- test-cpt-1
You are using one-to-many relationship between "Page" and "test-cpt-1", the relationship slug is "page-test-cpt-1"

you can try to add below codes into your theme file functions.php:

add_action( 'rest_api_init', 'create_api_posts_meta_field' );

function create_api_posts_meta_field() {
    register_rest_field( 'test-cpt-1', 'parent-page', array(  //replace test-cpt-1 with your child post type slug
           'get_callback'    => 'get_parent_page_for_api',
           'schema'          => null,
        )
    );
}

function get_parent_page_for_api( $object ) {
    //get the id of the post object array
    $post_id = $object['id'];
    //return the post meta
    return toolset_get_related_post( $post_id, 'page-test-cpt-1' ); // replace page-test-cpt-1 with one-to-many relationship slug
}

Above codes will add a custom field "parent-page" into the REST API result of single test-cpt-1, the value is parent page ID.

#1834415

I create this code in functions.php but doesn't work,

function create_api_posts_meta_field() {
    register_rest_field( 'productos', 'parent-page', array(
           'get_callback'    => 'get_parent_page_for_api',
           'schema'          => null,
        )
    );
}
 
function get_parent_page_for_api( $object ) {
    //get the id of the post object array
    $post_id = $object['id'];
    //return the post meta
    return toolset_get_related_post( $post_id, 'productos' );
}
/ 20201104134710
// <em><u>hidden link</u></em>

[
  {
    "id": 4675,
    "date": "2020-10-21T11:33:39",
    "date_gmt": "2020-10-21T11:33:39",
    "guid": {
      "rendered": "<em><u>hidden link</u></em>"
    },
    "modified": "2020-10-21T11:33:40",
    "modified_gmt": "2020-10-21T11:33:40",
    "slug": "producto-2",
    "status": "publish",
    "type": "productos",
    "link": "<em><u>hidden link</u></em>",
    "title": {
      "rendered": "Producto 2"
    },
    "content": {
      "rendered": "",
      "protected": false
    },
    "template": "",
    "_wpv_contains_gutenberg_views": "",
    "_edit_lock": "1603280930:1",
    "_edit_last": "1",
    "_views_template": "4635",
    "site-sidebar-layout": "default",
    "site-content-layout": "default",
    "theme-transparent-header-meta": "default",
    "wpcf-imagen-producto": "<em><u>hidden link</u></em>",
    "wpcf-nombre-producto": "Producto prueba 2",
    "wpcf-descripcion-producto": "blablabalablablablablablablablablablablabalablablablablablablablablablablabalablablablablablablablablablablabalablablablablablablablabla blablabalablablablablablablablabla blablabalablablablablablablablabla blablabalablablablablablablablabla blablabalablablablablablablablabla",
    "wpcf-precio-producto": "250",
    "uagb_featured_image_src": [
      
    ],
    "uagb_author_info": {
      "display_name": "user",
      "author_link": "<em><u>hidden link</u></em>"
    },
    "uagb_comment_info": 0,
    "uagb_excerpt": null,
    "parent-page": 4646,
    "toolset-meta": {
      "grupo-de-campo-para-productos": {
        "imagen-producto": {
          "type": "image",
          "raw": "<em><u>hidden link</u></em>",
          "attachment_id": 1088
        },
        "nombre-producto": {
          "type": "textfield",
          "raw": "Producto prueba 2"
        },
        "descripcion-producto": {
          "type": "wysiwyg",
          "raw": "blablabalablablablablablablablablablablabalablablablablablablablablablablabalablablablablablablablablablablabalablablablablablablablabla blablabalablablablablablablablabla blablabalablablablablablablablabla blablabalablablablablablablablabla blablabalablablablablablablablabla"
        },
        "precio-producto": {
          "type": "numeric",
          "raw": "250"
        }
      }
    },
    "_links": {
      "self": [
        {
          "href": "<em><u>hidden link</u></em>"
        }
      ],
      "collection": [
        {
          "href": "<em><u>hidden link</u></em>"
        }
      ],
      "about": [
        {
          "href": "<em><u>hidden link</u></em>"
        }
      ],
      "wp:attachment": [
        {
          "href": "<em><u>hidden link</u></em>"
        }
      ],
      "curies": [
        {
          "name": "wp",
          "href": "<em><u>hidden link</u></em>{rel}",
          "templated": true
        }
      ]
    }
  },
  {
    "id": 4668,
    "date": "2020-10-21T10:35:12",
    "date_gmt": "2020-10-21T10:35:12",
    "guid": {
      "rendered": "<em><u>hidden link</u></em>"
    },
    "modified": "2020-10-21T11:20:17",
    "modified_gmt": "2020-10-21T11:20:17",
    "slug": "producto-prueba",
    "status": "publish",
    "type": "productos",
    "link": "<em><u>hidden link</u></em>",
    "title": {
      "rendered": "Producto Prueba"
    },
    "content": {
      "rendered": "",
      "protected": false
    },
    "template": "",
    "_wpv_contains_gutenberg_views": "",
    "_edit_lock": "1603279717:1",
    "_edit_last": "1",
    "_views_template": "4635",
    "site-sidebar-layout": "default",
    "site-content-layout": "default",
    "theme-transparent-header-meta": "default",
    "wpcf-imagen-producto": "<em><u>hidden link</u></em>",
    "wpcf-nombre-producto": "Viaje de buceo",
    "wpcf-descripcion-producto": "lorem ipsum lorem ipsum lorem ipsum",
    "wpcf-precio-producto": "450",
    "uagb_featured_image_src": [
      
    ],
    "uagb_author_info": {
      "display_name": "user",
      "author_link": "<em><u>hidden link</u></em>"
    },
    "uagb_comment_info": 0,
    "uagb_excerpt": null,
    "parent-page": 4646,
    "toolset-meta": {
      "grupo-de-campo-para-productos": {
        "imagen-producto": {
          "type": "image",
          "raw": "<em><u>hidden link</u></em>",
          "attachment_id": 1090
        },
        "nombre-producto": {
          "type": "textfield",
          "raw": "Viaje de buceo"
        },
        "descripcion-producto": {
          "type": "wysiwyg",
          "raw": "lorem ipsum lorem ipsum lorem ipsum"
        },
        "precio-producto": {
          "type": "numeric",
          "raw": "450"
        }
      }
    },
    "_links": {
      "self": [
        {
          "href": "<em><u>hidden link</u></em>"
        }
      ],
      "collection": [
        {
          "href": "<em><u>hidden link</u></em>"
        }
      ],
      "about": [
        {
          "href": "<em><u>hidden link</u></em>"
        }
      ],
      "wp:attachment": [
        {
          "href": "<em><u>hidden link</u></em>"
        }
      ],
      "curies": [
        {
          "name": "wp",
          "href": "<em><u>hidden link</u></em>{rel}",
          "templated": true
        }
      ]
    }
  },
  {
    "id": 4632,
    "date": "2020-10-19T16:40:14",
    "date_gmt": "2020-10-19T16:40:14",
    "guid": {
      "rendered": "<em><u>hidden link</u></em>"
    },
    "modified": "2020-10-21T10:34:14",
    "modified_gmt": "2020-10-21T10:34:14",
    "slug": "producto-1",
    "status": "publish",
    "type": "productos",
    "link": "<em><u>hidden link</u></em>",
    "title": {
      "rendered": "Producto 1"
    },
    "content": {
      "rendered": "",
      "protected": false
    },
    "template": "",
    "_wpv_contains_gutenberg_views": "",
    "_edit_lock": "1603279737:1",
    "_edit_last": "1",
    "_views_template": "4635",
    "site-sidebar-layout": "default",
    "site-content-layout": "default",
    "theme-transparent-header-meta": "default",
    "wpcf-imagen-producto": "<em><u>hidden link</u></em>",
    "wpcf-nombre-producto": "Producto 1",
    "wpcf-descripcion-producto": "lorem ipsum lorem ipsum lorem ipsum",
    "wpcf-precio-producto": "11",
    "uagb_featured_image_src": [
      
    ],
    "uagb_author_info": {
      "display_name": "user",
      "author_link": "<em><u>hidden link</u></em>"
    },
    "uagb_comment_info": 0,
    "uagb_excerpt": null,
    "parent-page": 4595,
    "toolset-meta": {
      "grupo-de-campo-para-productos": {
        "imagen-producto": {
          "type": "image",
          "raw": "<em><u>hidden link</u></em>",
          "attachment_id": 250
        },
        "nombre-producto": {
          "type": "textfield",
          "raw": "Producto 1"
        },
        "descripcion-producto": {
          "type": "wysiwyg",
          "raw": "lorem ipsum lorem ipsum lorem ipsum"
        },
        "precio-producto": {
          "type": "numeric",
          "raw": "11"
        }
      }
    },
    "_links": {
      "self": [
        {
          "href": "<em><u>hidden link</u></em>"
        }
      ],
      "collection": [
        {
          "href": "<em><u>hidden link</u></em>"
        }
      ],
      "about": [
        {
          "href": "<em><u>hidden link</u></em>"
        }
      ],
      "wp:attachment": [
        {
          "href": "<em><u>hidden link</u></em>"
        }
      ],
      "curies": [
        {
          "name": "wp",
          "href": "<em><u>hidden link</u></em>{rel}",
          "templated": true
        }
      ]
    }
  }
]
/ 20201104134229
// <em><u>hidden link</u></em>

[
  {
    "id": 4646,
    "date": "2020-10-19T19:07:40",
    "date_gmt": "2020-10-19T19:07:40",
    "guid": {
      "rendered": "<em><u>hidden link</u></em>"
    },
    "modified": "2020-11-01T16:57:49",
    "modified_gmt": "2020-11-01T16:57:49",
    "slug": "prueba-4",
    "status": "publish",
    "type": "comercios",
    "link": "<em><u>hidden link</u></em>",
    "title": {
      "rendered": "Comercio prueba 4"
    },
    "content": {
      "rendered": "",
      "protected": false
    },
    "template": "",
    "zonas": [
      26
    ],
    "catcomercio": [
      70
    ],
    "negocios": [
      88
    ],
    "restaurante": [
      97
    ],
    "servicios": [
      105,
      112
    ],
    "_wp_page_template": "default",
    "_wpv_contains_gutenberg_views": "",
    "_views_template": "4599",
    "_edit_lock": "1604249788:1",
    "_edit_last": "1",
    "_wpcf-direccion-comercio-sort-order": [
      7508
    ],
    "_wpcf-telefono-de-contacto-sort-order": [
      7509
    ],
    "_wpcf-galeria-de-imagenes-sort-order": [
      7511,
      7512,
      7513,
      7514,
      7515
    ],
    "site-sidebar-layout": "no-sidebar",
    "site-content-layout": "default",
    "theme-transparent-header-meta": "default",
    "site-post-title": "disabled",
    "wpcf-nombre_del_comercio": "Nombre de Comercio 1",
    "wpcf-descripcion_del_negocio": "Lorem Ipsum, Lorem ipsum, Lorem ipsum Lorem Ipsum, Lorem ipsum, Lorem ipsum Lorem Ipsum, Lorem ipsum, Lorem ipsum Lorem Ipsum, Lorem ipsum, Lorem ipsum Lorem Ipsum, Lorem ipsum, Lorem ipsum Lorem Ipsum, Lorem ipsum, Lorem ipsumLorem Ipsum, Lorem ipsum, Lorem  Lorem Ipsum, Lorem ipsum, Lorem ipsum Lorem Ipsum, Lorem ipsum, Lorem ipsum",
    "wpcf-direccion_comercio": "Calle Caramanta, 4, Madrid, España",
    "wpcf-telefono_de_contacto": "663454792",
    "wpcf-logo": "<em><u>hidden link</u></em>",
    "wpcf-galeria_imagenes": "<em><u>hidden link</u></em>",
    "wpcf-sitio_web": "<em><u>hidden link</u></em>",
    "wpcf-horario_simple": "L-V: 10 a 19 Sab: 10 a 14",
    "wpcf-descripcion_corta": "Descripción corta para la ficha de producto del buscador para aparecer en el listado de comercios",
    "ast-below-header-display": "",
    "footer-adv-display": "",
    "adv-header-id-meta": "",
    "stick-header-meta": "",
    "header-above-stick-meta": "",
    "header-main-stick-meta": "",
    "header-below-stick-meta": "",
    "_oembed_d204a63d9b3aca71a2333e0b08c3bc86": "{{unknown}}",
    "_oembed_ffbdd56aca52851d5a9e0d735274da77": "{{unknown}}",
    "_wpcf-direccion_comercio-sort-order": [
      8182
    ],
    "_wpcf-telefono_de_contacto-sort-order": [
      8183
    ],
    "_wpcf-galeria_imagenes-sort-order": [
      8185,
      8186
    ],
    "ast-breadcrumbs-content": "disabled",
    "wpcf_tipo_de_negocio": "1",
    "uagb_featured_image_src": [
      
    ],
    "uagb_author_info": {
      "display_name": "user",
      "author_link": "<em><u>hidden link</u></em>"
    },
    "uagb_comment_info": 0,
    "uagb_excerpt": null,
    "toolset-meta": {
      "grupo-de-campo-para-comercios": [
        
      ],
      "comercios": {
        "tipo-de-negocio": {
          "type": "select",
          "raw": "1"
        },
        "nombre_del_comercio": {
          "type": "textfield",
          "raw": "Nombre de Comercio 1"
        },
        "descripcion_del_negocio": {
          "type": "wysiwyg",
          "raw": "Lorem Ipsum, Lorem ipsum, Lorem ipsum Lorem Ipsum, Lorem ipsum, Lorem ipsum Lorem Ipsum, Lorem ipsum, Lorem ipsum Lorem Ipsum, Lorem ipsum, Lorem ipsum Lorem Ipsum, Lorem ipsum, Lorem ipsum Lorem Ipsum, Lorem ipsum, Lorem ipsumLorem Ipsum, Lorem ipsum, Lorem  Lorem Ipsum, Lorem ipsum, Lorem ipsum Lorem Ipsum, Lorem ipsum, Lorem ipsum"
        },
        "direccion_comercio": {
          "type": "google_address",
          "raw": [
            "Calle Caramanta, 4, Madrid, España"
          ]
        },
        "telefono_de_contacto": {
          "type": "phone",
          "raw": [
            "663454792"
          ]
        },
        "logo": {
          "type": "image",
          "raw": "<em><u>hidden link</u></em>",
          "attachment_id": 421
        },
        "galeria_imagenes": {
          "type": "image",
          "raw": [
            "<em><u>hidden link</u></em>",
            "<em><u>hidden link</u></em>"
          ],
          "repeatable": [
            {
              "raw": "<em><u>hidden link</u></em>",
              "attachment_id": 1117
            },
            {
              "raw": "<em><u>hidden link</u></em>",
              "attachment_id": 4936
            }
          ]
        },
        "sitio_web": {
          "type": "url",
          "raw": "<em><u>hidden link</u></em>"
        },
        "horario_simple": {
          "type": "textfield",
          "raw": "L-V: 10 a 19 Sab: 10 a 14"
        },
        "descripcion_corta": {
          "type": "textfield",
          "raw": "Descripción corta para la ficha de producto del buscador para aparecer en el listado de comercios"
        }
      }
    },
    "_links": {
      "self": [
        {
          "href": "<em><u>hidden link</u></em>"
        }
      ],
      "collection": [
        {
          "href": "<em><u>hidden link</u></em>"
        }
      ],
      "about": [
        {
          "href": "<em><u>hidden link</u></em>"
        }
      ],
      "version-history": [
        {
          "count": 3,
          "href": "<em><u>hidden link</u></em>"
        }
      ],
      "predecessor-version": [
        {
          "id": 4910,
          "href": "<em><u>hidden link</u></em>"
        }
      ],
      "wp:attachment": [
        {
          "href": "<em><u>hidden link</u></em>"
        }
      ],
      "wp:term": [
        {
          "taxonomy": "zonas",
          "embeddable": true,
          "href": "<em><u>hidden link</u></em>"
        },
        {
          "taxonomy": "catcomercio",
          "embeddable": true,
          "href": "<em><u>hidden link</u></em>"
        },
        {
          "taxonomy": "negocios",
          "embeddable": true,
          "href": "<em><u>hidden link</u></em>"
        },
        {
          "taxonomy": "restaurante",
          "embeddable": true,
          "href": "<em><u>hidden link</u></em>"
        },
        {
          "taxonomy": "servicios",
          "embeddable": true,
          "href": "<em><u>hidden link</u></em>"
        }
      ],
      "curies": [
        {
          "name": "wp",
          "href": "<em><u>hidden link</u></em>{rel}",
          "templated": true
        }
      ]
    }
  },
  {
    "id": 4645,
    "date": "2020-10-19T19:07:37",
    "date_gmt": "2020-10-19T19:07:37",
    "guid": {
      "rendered": "<em><u>hidden link</u></em>"
    },
    "modified": "2020-10-19T19:14:37",
    "modified_gmt": "2020-10-19T19:14:37",
    "slug": "prueba-3",
    "status": "publish",
    "type": "comercios",
    "link": "<em><u>hidden link</u></em>",
    "title": {
      "rendered": "Prueba 3"
    },
    "content": {
      "rendered": "",
      "protected": false
    },
    "template": "",
    "zonas": [
      
    ],
    "catcomercio": [
      
    ],
    "negocios": [
      
    ],
    "restaurante": [
      
    ],
    "servicios": [
      
    ],
    "_wp_page_template": "default",
    "_wpv_contains_gutenberg_views": "",
    "_views_template": "4599",
    "_edit_lock": "1603134877:1",
    "_edit_last": "1",
    "_wpcf-direccion-comercio-sort-order": [
      7317
    ],
    "_wpcf-telefono-de-contacto-sort-order": [
      7318
    ],
    "_wpcf-galeria-de-imagenes-sort-order": [
      7320,
      7321,
      7322,
      7323,
      7324
    ],
    "site-sidebar-layout": "no-sidebar",
    "site-content-layout": "default",
    "theme-transparent-header-meta": "default",
    "site-post-title": "disabled",
    "wpcf-nombre_del_comercio": "Nombre de Comercio 1",
    "wpcf-descripcion_del_negocio": "Lorem Ipsum, Lorem ipsum, Lorem ipsum Lorem Ipsum, Lorem ipsum, Lorem ipsum Lorem Ipsum, Lorem ipsum, Lorem ipsum Lorem Ipsum, Lorem ipsum, Lorem ipsum Lorem Ipsum, Lorem ipsum, Lorem ipsum Lorem Ipsum, Lorem ipsum, Lorem ipsumLorem Ipsum, Lorem ipsum, Lorem  Lorem Ipsum, Lorem ipsum, Lorem ipsum Lorem Ipsum, Lorem ipsum, Lorem ipsum",
    "wpcf-direccion_comercio": "Calle Caramanta, 4, Madrid, España",
    "wpcf-telefono_de_contacto": "663454792",
    "wpcf-logo": "<em><u>hidden link</u></em>",
    "wpcf-galeria_imagenes": "<em><u>hidden link</u></em>",
    "wpcf-sitio_web": "<em><u>hidden link</u></em>",
    "wpcf-horario_simple": "L-V: 10 a 19 Sab: 10 a 14",
    "wpcf-descripcion_corta": "Descripción corta para la ficha de producto del buscador",
    "ast-below-header-display": "",
    "footer-adv-display": "",
    "adv-header-id-meta": "",
    "stick-header-meta": "",
    "header-above-stick-meta": "",
    "header-main-stick-meta": "",
    "header-below-stick-meta": "",
    "_oembed_d204a63d9b3aca71a2333e0b08c3bc86": "{{unknown}}",
    "_oembed_ffbdd56aca52851d5a9e0d735274da77": "{{unknown}}",
    "_wpcf-direccion_comercio-sort-order": "",
    "_wpcf-telefono_de_contacto-sort-order": "",
    "_wpcf-galeria_imagenes-sort-order": "",
    "ast-breadcrumbs-content": "",
    "wpcf_tipo_de_negocio": "",
    "uagb_featured_image_src": [
      
    ],
    "uagb_author_info": {
      "display_name": "user",
      "author_link": "<em><u>hidden link</u></em>"
    },
    "uagb_comment_info": 0,
    "uagb_excerpt": null,
    "toolset-meta": {
      "grupo-de-campo-para-comercios": [
        
      ],
      "comercios": {
        "tipo-de-negocio": {
          "type": "select",
          "raw": ""
        },
        "nombre_del_comercio": {
          "type": "textfield",
          "raw": "Nombre de Comercio 1"
        },
        "descripcion_del_negocio": {
          "type": "wysiwyg",
          "raw": "Lorem Ipsum, Lorem ipsum, Lorem ipsum Lorem Ipsum, Lorem ipsum, Lorem ipsum Lorem Ipsum, Lorem ipsum, Lorem ipsum Lorem Ipsum, Lorem ipsum, Lorem ipsum Lorem Ipsum, Lorem ipsum, Lorem ipsum Lorem Ipsum, Lorem ipsum, Lorem ipsumLorem Ipsum, Lorem ipsum, Lorem  Lorem Ipsum, Lorem ipsum, Lorem ipsum Lorem Ipsum, Lorem ipsum, Lorem ipsum"
        },
        "direccion_comercio": {
          "type": "google_address",
          "raw": [
            "Calle Caramanta, 4, Madrid, España"
          ]
        },
        "telefono_de_contacto": {
          "type": "phone",
          "raw": [
            "663454792"
          ]
        },
        "logo": {
          "type": "image",
          "raw": "<em><u>hidden link</u></em>",
          "attachment_id": 421
        },
        "galeria_imagenes": {
          "type": "image",
          "raw": [
            "<em><u>hidden link</u></em>",
            "<em><u>hidden link</u></em>",
            "<em><u>hidden link</u></em>",
            "<em><u>hidden link</u></em>",
            "<em><u>hidden link</u></em>"
          ],
          "repeatable": [
            {
              "raw": "<em><u>hidden link</u></em>",
              "attachment_id": 1307
            },
            {
              "raw": "<em><u>hidden link</u></em>",
              "attachment_id": 1117
            },
            {
              "raw": "<em><u>hidden link</u></em>",
              "attachment_id": 1112
            },
            {
              "raw": "<em><u>hidden link</u></em>",
              "attachment_id": 1106
            },
            {
              "raw": "<em><u>hidden link</u></em>",
              "attachment_id": 1103
            }
          ]
        },
        "sitio_web": {
          "type": "url",
          "raw": "<em><u>hidden link</u></em>"
        },
        "horario_simple": {
          "type": "textfield",
          "raw": "L-V: 10 a 19 Sab: 10 a 14"
        },
        "descripcion_corta": {
          "type": "textfield",
          "raw": "Descripción corta para la ficha de producto del buscador"
        }
      }
    },
    "_links": {
      "self": [
        {
          "href": "<em><u>hidden link</u></em>"
        }
      ],
      "collection": [
        {
          "href": "<em><u>hidden link</u></em>"
        }
      ],
      "about": [
        {
          "href": "<em><u>hidden link</u></em>"
        }
      ],
      "version-history": [
        {
          "count": 1,
          "href": "<em><u>hidden link</u></em>"
        }
      ],
      "predecessor-version": [
        {
          "id": 4648,
          "href": "<em><u>hidden link</u></em>"
        }
      ],
      "wp:attachment": [
        {
          "href": "<em><u>hidden link</u></em>"
        }
      ],
      "wp:term": [
        {
          "taxonomy": "zonas",
          "embeddable": true,
          "href": "<em><u>hidden link</u></em>"
        },
        {
          "taxonomy": "catcomercio",
          "embeddable": true,
          "href": "<em><u>hidden link</u></em>"
        },
        {
          "taxonomy": "negocios",
          "embeddable": true,
          "href": "<em><u>hidden link</u></em>"
        },
        {
          "taxonomy": "restaurante",
          "embeddable": true,
          "href": "<em><u>hidden link</u></em>"
        },
        {
          "taxonomy": "servicios",
          "embeddable": true,
          "href": "<em><u>hidden link</u></em>"
        }
      ],
      "curies": [
        {
          "name": "wp",
          "href": "<em><u>hidden link</u></em>{rel}",
          "templated": true
        }
      ]
    }
  },
  {
    "id": 4644,
    "date": "2020-10-19T19:07:34",
    "date_gmt": "2020-10-19T19:07:34",
    "guid": {
      "rendered": "<em><u>hidden link</u></em>"
    },
    "modified": "2020-10-19T19:14:43",
    "modified_gmt": "2020-10-19T19:14:43",
    "slug": "prueba-2",
    "status": "publish",
    "type": "comercios",
    "link": "<em><u>hidden link</u></em>",
    "title": {
      "rendered": "Prueba 2"
    },
    "content": {
      "rendered": "",
      "protected": false
    },
    "template": "",
    "zonas": [
      
    ],
    "catcomercio": [
      
    ],
    "negocios": [
      
    ],
    "restaurante": [
      
    ],
    "servicios": [
      
    ],
    "_wp_page_template": "default",
    "_wpv_contains_gutenberg_views": "",
    "_views_template": "4599",
    "_edit_lock": "1603134883:1",
    "_edit_last": "1",
    "_wpcf-direccion-comercio-sort-order": [
      7317
    ],
    "_wpcf-telefono-de-contacto-sort-order": [
      7318
    ],
    "_wpcf-galeria-de-imagenes-sort-order": [
      7320,
      7321,
      7322,
      7323,
      7324
    ],
    "site-sidebar-layout": "no-sidebar",
    "site-content-layout": "default",
    "theme-transparent-header-meta": "default",
    "site-post-title": "disabled",
    "wpcf-nombre_del_comercio": "Nombre de Comercio 1",
    "wpcf-descripcion_del_negocio": "Lorem Ipsum, Lorem ipsum, Lorem ipsum Lorem Ipsum, Lorem ipsum, Lorem ipsum Lorem Ipsum, Lorem ipsum, Lorem ipsum Lorem Ipsum, Lorem ipsum, Lorem ipsum Lorem Ipsum, Lorem ipsum, Lorem ipsum Lorem Ipsum, Lorem ipsum, Lorem ipsumLorem Ipsum, Lorem ipsum, Lorem  Lorem Ipsum, Lorem ipsum, Lorem ipsum Lorem Ipsum, Lorem ipsum, Lorem ipsum",
    "wpcf-direccion_comercio": "Calle Caramanta, 4, Madrid, España",
    "wpcf-telefono_de_contacto": "663454792",
    "wpcf-logo": "<em><u>hidden link</u></em>",
    "wpcf-galeria_imagenes": "<em><u>hidden link</u></em>",
    "wpcf-sitio_web": "<em><u>hidden link</u></em>",
    "wpcf-horario_simple": "L-V: 10 a 19 Sab: 10 a 14",
    "wpcf-descripcion_corta": "Descripción corta para la ficha de producto del buscador",
    "ast-below-header-display": "",
    "footer-adv-display": "",
    "adv-header-id-meta": "",
    "stick-header-meta": "",
    "header-above-stick-meta": "",
    "header-main-stick-meta": "",
    "header-below-stick-meta": "",
    "_oembed_d204a63d9b3aca71a2333e0b08c3bc86": "{{unknown}}",
    "_oembed_ffbdd56aca52851d5a9e0d735274da77": "{{unknown}}",
    "_wpcf-direccion_comercio-sort-order": "",
    "_wpcf-telefono_de_contacto-sort-order": "",
    "_wpcf-galeria_imagenes-sort-order": "",
    "ast-breadcrumbs-content": "",
    "wpcf_tipo_de_negocio": "",
    "uagb_featured_image_src": [
      
    ],
    "uagb_author_info": {
      "display_name": "user",
      "author_link": "<em><u>hidden link</u></em>"
    },
    "uagb_comment_info": 0,
    "uagb_excerpt": null,
    "toolset-meta": {
      "grupo-de-campo-para-comercios": [
        
      ],
      "comercios": {
        "tipo-de-negocio": {
          "type": "select",
          "raw": ""
        },
        "nombre_del_comercio": {
          "type": "textfield",
          "raw": "Nombre de Comercio 1"
        },
        "descripcion_del_negocio": {
          "type": "wysiwyg",
          "raw": "Lorem Ipsum, Lorem ipsum, Lorem ipsum Lorem Ipsum, Lorem ipsum, Lorem ipsum Lorem Ipsum, Lorem ipsum, Lorem ipsum Lorem Ipsum, Lorem ipsum, Lorem ipsum Lorem Ipsum, Lorem ipsum, Lorem ipsum Lorem Ipsum, Lorem ipsum, Lorem ipsumLorem Ipsum, Lorem ipsum, Lorem  Lorem Ipsum, Lorem ipsum, Lorem ipsum Lorem Ipsum, Lorem ipsum, Lorem ipsum"
        },
        "direccion_comercio": {
          "type": "google_address",
          "raw": [
            "Calle Caramanta, 4, Madrid, España"
          ]
        },
        "telefono_de_contacto": {
          "type": "phone",
          "raw": [
            "663454792"
          ]
        },
        "logo": {
          "type": "image",
          "raw": "<em><u>hidden link</u></em>",
          "attachment_id": 421
        },
        "galeria_imagenes": {
          "type": "image",
          "raw": [
            "<em><u>hidden link</u></em>",
            "<em><u>hidden link</u></em>",
            "<em><u>hidden link</u></em>",
            "<em><u>hidden link</u></em>",
            "<em><u>hidden link</u></em>"
          ],
          "repeatable": [
            {
              "raw": "<em><u>hidden link</u></em>",
              "attachment_id": 1307
            },
            {
              "raw": "<em><u>hidden link</u></em>",
              "attachment_id": 1117
            },
            {
              "raw": "<em><u>hidden link</u></em>",
              "attachment_id": 1112
            },
            {
              "raw": "<em><u>hidden link</u></em>",
              "attachment_id": 1106
            },
            {
              "raw": "<em><u>hidden link</u></em>",
              "attachment_id": 1103
            }
          ]
        },
        "sitio_web": {
          "type": "url",
          "raw": "<em><u>hidden link</u></em>"
        },
        "horario_simple": {
          "type": "textfield",
          "raw": "L-V: 10 a 19 Sab: 10 a 14"
        },
        "descripcion_corta": {
          "type": "textfield",
          "raw": "Descripción corta para la ficha de producto del buscador"
        }
      }
    },
    "_links": {
      "self": [
        {
          "href": "<em><u>hidden link</u></em>"
        }
      ],
      "collection": [
        {
          "href": "<em><u>hidden link</u></em>"
        }
      ],
      "about": [
        {
          "href": "<em><u>hidden link</u></em>"
        }
      ],
      "version-history": [
        {
          "count": 1,
          "href": "<em><u>hidden link</u></em>"
        }
      ],
      "predecessor-version": [
        {
          "id": 4647,
          "href": "<em><u>hidden link</u></em>"
        }
      ],
      "wp:attachment": [
        {
          "href": "<em><u>hidden link</u></em>"
        }
      ],
      "wp:term": [
        {
          "taxonomy": "zonas",
          "embeddable": true,
          "href": "<em><u>hidden link</u></em>"
        },
        {
          "taxonomy": "catcomercio",
          "embeddable": true,
          "href": "<em><u>hidden link</u></em>"
        },
        {
          "taxonomy": "negocios",
          "embeddable": true,
          "href": "<em><u>hidden link</u></em>"
        },
        {
          "taxonomy": "restaurante",
          "embeddable": true,
          "href": "<em><u>hidden link</u></em>"
        },
        {
          "taxonomy": "servicios",
          "embeddable": true,
          "href": "<em><u>hidden link</u></em>"
        }
      ],
      "curies": [
        {
          "name": "wp",
          "href": "<em><u>hidden link</u></em>{rel}",
          "templated": true
        }
      ]
    }
  },
  {
    "id": 4595,
    "date": "2020-10-19T12:31:06",
    "date_gmt": "2020-10-19T12:31:06",
    "guid": {
      "rendered": "<em><u>hidden link</u></em>"
    },
    "modified": "2020-10-19T19:14:48",
    "modified_gmt": "2020-10-19T19:14:48",
    "slug": "prueba-1",
    "status": "publish",
    "type": "comercios",
    "link": "<em><u>hidden link</u></em>",
    "title": {
      "rendered": "Prueba 1"
    },
    "content": {
      "rendered": "",
      "protected": false
    },
    "template": "",
    "zonas": [
      
    ],
    "catcomercio": [
      
    ],
    "negocios": [
      
    ],
    "restaurante": [
      
    ],
    "servicios": [
      
    ],
    "_wp_page_template": "default",
    "_wpv_contains_gutenberg_views": "",
    "_views_template": "4599",
    "_edit_lock": "1603134888:1",
    "_edit_last": "1",
    "_wpcf-direccion-comercio-sort-order": [
      7317
    ],
    "_wpcf-telefono-de-contacto-sort-order": [
      7318
    ],
    "_wpcf-galeria-de-imagenes-sort-order": [
      7320,
      7321,
      7322,
      7323,
      7324
    ],
    "site-sidebar-layout": "no-sidebar",
    "site-content-layout": "default",
    "theme-transparent-header-meta": "default",
    "site-post-title": "disabled",
    "wpcf-nombre_del_comercio": "Nombre de Comercio 1",
    "wpcf-descripcion_del_negocio": "Lorem Ipsum, Lorem ipsum, Lorem ipsum Lorem Ipsum, Lorem ipsum, Lorem ipsum Lorem Ipsum, Lorem ipsum, Lorem ipsum Lorem Ipsum, Lorem ipsum, Lorem ipsum Lorem Ipsum, Lorem ipsum, Lorem ipsum Lorem Ipsum, Lorem ipsum, Lorem ipsumLorem Ipsum, Lorem ipsum, Lorem  Lorem Ipsum, Lorem ipsum, Lorem ipsum Lorem Ipsum, Lorem ipsum, Lorem ipsum",
    "wpcf-direccion_comercio": "Calle Caramanta, 4, Madrid, España",
    "wpcf-telefono_de_contacto": "663454792",
    "wpcf-logo": "<em><u>hidden link</u></em>",
    "wpcf-galeria_imagenes": "<em><u>hidden link</u></em>",
    "wpcf-sitio_web": "<em><u>hidden link</u></em>",
    "wpcf-horario_simple": "L-V: 10 a 19 Sab: 10 a 14",
    "wpcf-descripcion_corta": "Descripción corta para la ficha de producto del buscador",
    "ast-below-header-display": "",
    "footer-adv-display": "",
    "adv-header-id-meta": "",
    "stick-header-meta": "",
    "header-above-stick-meta": "",
    "header-main-stick-meta": "",
    "header-below-stick-meta": "",
    "_oembed_d204a63d9b3aca71a2333e0b08c3bc86": "{{unknown}}",
    "_oembed_ffbdd56aca52851d5a9e0d735274da77": "{{unknown}}",
    "_wpcf-direccion_comercio-sort-order": "",
    "_wpcf-telefono_de_contacto-sort-order": "",
    "_wpcf-galeria_imagenes-sort-order": "",
    "ast-breadcrumbs-content": "",
    "wpcf_tipo_de_negocio": "",
    "uagb_featured_image_src": [
      
    ],
    "uagb_author_info": {
      "display_name": "user",
      "author_link": "<em><u>hidden link</u></em>"
    },
    "uagb_comment_info": 0,
    "uagb_excerpt": null,
    "toolset-meta": {
      "grupo-de-campo-para-comercios": [
        
      ],
      "comercios": {
        "tipo-de-negocio": {
          "type": "select",
          "raw": ""
        },
        "nombre_del_comercio": {
          "type": "textfield",
          "raw": "Nombre de Comercio 1"
        },
        "descripcion_del_negocio": {
          "type": "wysiwyg",
          "raw": "Lorem Ipsum, Lorem ipsum, Lorem ipsum Lorem Ipsum, Lorem ipsum, Lorem ipsum Lorem Ipsum, Lorem ipsum, Lorem ipsum Lorem Ipsum, Lorem ipsum, Lorem ipsum Lorem Ipsum, Lorem ipsum, Lorem ipsum Lorem Ipsum, Lorem ipsum, Lorem ipsumLorem Ipsum, Lorem ipsum, Lorem  Lorem Ipsum, Lorem ipsum, Lorem ipsum Lorem Ipsum, Lorem ipsum, Lorem ipsum"
        },
        "direccion_comercio": {
          "type": "google_address",
          "raw": [
            "Calle Caramanta, 4, Madrid, España"
          ]
        },
        "telefono_de_contacto": {
          "type": "phone",
          "raw": [
            "663454792"
          ]
        },
        "logo": {
          "type": "image",
          "raw": "<em><u>hidden link</u></em>",
          "attachment_id": 421
        },
        "galeria_imagenes": {
          "type": "image",
          "raw": [
            "<em><u>hidden link</u></em>",
            "<em><u>hidden link</u></em>",
            "<em><u>hidden link</u></em>",
            "<em><u>hidden link</u></em>",
            "<em><u>hidden link</u></em>"
          ],
          "repeatable": [
            {
              "raw": "<em><u>hidden link</u></em>",
              "attachment_id": 1307
            },
            {
              "raw": "<em><u>hidden link</u></em>",
              "attachment_id": 1117
            },
            {
              "raw": "<em><u>hidden link</u></em>",
              "attachment_id": 1112
            },
            {
              "raw": "<em><u>hidden link</u></em>",
              "attachment_id": 1106
            },
            {
              "raw": "<em><u>hidden link</u></em>",
              "attachment_id": 1103
            }
          ]
        },
        "sitio_web": {
          "type": "url",
          "raw": "<em><u>hidden link</u></em>"
        },
        "horario_simple": {
          "type": "textfield",
          "raw": "L-V: 10 a 19 Sab: 10 a 14"
        },
        "descripcion_corta": {
          "type": "textfield",
          "raw": "Descripción corta para la ficha de producto del buscador"
        }
      }
    },
    "_links": {
      "self": [
        {
          "href": "<em><u>hidden link</u></em>"
        }
      ],
      "collection": [
        {
          "href": "<em><u>hidden link</u></em>"
        }
      ],
      "about": [
        {
          "href": "<em><u>hidden link</u></em>"
        }
      ],
      "version-history": [
        {
          "count": 1,
          "href": "<em><u>hidden link</u></em>"
        }
      ],
      "predecessor-version": [
        {
          "id": 4641,
          "href": "<em><u>hidden link</u></em>"
        }
      ],
      "wp:attachment": [
        {
          "href": "<em><u>hidden link</u></em>"
        }
      ],
      "wp:term": [
        {
          "taxonomy": "zonas",
          "embeddable": true,
          "href": "<em><u>hidden link</u></em>"
        },
        {
          "taxonomy": "catcomercio",
          "embeddable": true,
          "href": "<em><u>hidden link</u></em>"
        },
        {
          "taxonomy": "negocios",
          "embeddable": true,
          "href": "<em><u>hidden link</u></em>"
        },
        {
          "taxonomy": "restaurante",
          "embeddable": true,
          "href": "<em><u>hidden link</u></em>"
        },
        {
          "taxonomy": "servicios",
          "embeddable": true,
          "href": "<em><u>hidden link</u></em>"
        }
      ],
      "curies": [
        {
          "name": "wp",
          "href": "<em><u>hidden link</u></em>{rel}",
          "templated": true
        }
      ]
    }
  }
]
#1834981

I have checked the result you provided, there are some items, for example:
"parent-page": 4646,
...
"parent-page": 4595,

Are they expected result? if they are not, what result do you expect to see?

#1835315

My issue is resolved now. Thank you!

#1835317

Oh! great!

I cant see in code, sorry. Relation this I have other question.... Is it possible add
google address coordinates in json?

Thanks for your help.

New threads created by Luo Yang and linked to this one are listed below:

https://toolset.com/forums/topic/is-it-possible-add-google-address-coordinates-in-json/

#1836015

I assume the original questions of this thread is resolved, according to our support policy, we prefer one ticket one question, for other new questions, please check the new thread here:
https://toolset.com/forums/topic/is-it-possible-add-google-address-coordinates-in-json/

#1844463

Hi! My web is now in real, and this code doesn't work.

Could you help me?

Thanks

#1844695

If I want to create other relationship, How i can add action?

If I repeat this code with other custom type 'promociones' said its not posible because have to replace...

//AÑADIR RELATIONSHIPS PRODUCTOS Y PROMOCIONES
add_action( 'rest_api_init', 'create_api_posts_meta_field' );
 
function create_api_posts_meta_field() {
    register_rest_field( 'productos','promociones', 'parent-page', array(
           'get_callback'    => 'get_parent_page_for_api',
           'schema'          => null,
        )
    );
}
 
function get_parent_page_for_api( $object ) {
    //get the id of the post object array
    $post_id = $object['id'];
    //return the post meta
    return toolset_get_related_post( $post_id, 'productos','promociones' );
}
#1846413

I think they should be done separately, don't you?

But it's wrong

//AÑADIR RELATIONSHIPS PROMOCIONES
add_action( 'rest_api_init', 'create_api_posts_meta_field');
 
function create_api_posts_meta_field() {
    register_rest_field( 'promociones', 'parent-page', array(
           'get_callback'    => 'get_parent_page_for_api',
           'schema'          => null,
        )
    );
}
 
function get_parent_page_for_api( $object ) {
    //get the id of the post object array
    $post_id = $object['id'];
    //return the post meta
    return toolset_get_related_post( $post_id, 'promociones');
}

//AÑADIR RELATIONSHIPS BONOS
add_action( 'rest_api_init', 'create_api_posts_meta_field');
 
function create_api_posts_meta_field() {
    register_rest_field( 'bonos', 'parent-page', array(
           'get_callback'    => 'get_parent_page_for_api',
           'schema'          => null,
        )
    );
}
 
function get_parent_page_for_api( $object ) {
    //get the id of the post object array
    $post_id = $object['id'];
    //return the post meta
    return toolset_get_related_post( $post_id, 'bonos');
}