Help Center

How to Get and Update Relation Data via REST API

How to Get and Update Relation Data via REST API

Discover how to get the relation data list and update or replace the children or parent items via REST API with the JetEngine WordPress plugin.

The Public REST API for Relations feature allows you to get and update relations data through REST API with the help of the JetEngine WordPress plugin.

Public REST API for Relations Settings Overview

Navigate to the JetEngine > Relations tab and click to edit the needed relation.

We will work with the “Guides > Tours” (Users > Posts) relation.

relations list

Go to the Register get items/item REST API Endpoint toggle and enable it.

register get items REST API endpoint

In the Endpoint URL field, you will see three URLs that can be copied and used for the connection between two websites:

  • GET – /wp-json/jet-rel/<relation ID> — retrieve all data for the selected relation;
  • GET – /wp-json/jet-rel/<relation ID>/children/<item ID> — retrieve child items for the selected item ID;
  • GET – /wp-json/jet-rel/<relation ID>/parents/<item ID> — retrieve parent items for the selected item ID.

In the Access Capability field, you can choose who will be allowed to access these related items. By default, there are no restrictions, but if you enter, for example, “edit_pages” capability, only editors and admins would be able to edit them.

Enable the Register update REST API Endpoint toggle.

register update REST API endpoint

POST – /wp-json/jet-rel/<relation ID> — specify the required data. The body of the query post must contain the following data:

{
	parent_id: ID/IDs,
	child_id: ID/IDs,
	context: child/parent,
	store_items_type: replace/update
}

Where you need to enter the ID/IDs of the parent item/items, ID/IDs of the child item/items; context can be “child” or “parent,” where “child” means updating the child item from the parent and “parent” – updating the parent item from the child. The store_items_type row can be “replace” or “update,” where “replace” deletes existing elements and insert the new ones instead when updating, and “update” adds the new elements to the existing ones.

When you finish, hit the “Update Relation” button.

How to Update Relation Data via REST API

We will show how to update and replace related items on the example of the Advanced REST client application.

We select the “POST” Method and enter the relation endpoint URL in the Request URL field.

In the Headers tab, we add a Header with the “Content-Type” Header name and “application/json” Header value. If you want, you can also add the “Authorization” Header.

advanced REST client headers

In the Body tab, we set the “application/json” Body content type and “Raw input” Editor view.

Updating the child item from the parent

We decided to add a new child tour to the parent guide.

parent user

We enter such a JSON code:

 {
  "parent_id": 2,
  "child_id": 1310,
  "context": "child",
  "store_items_type": "update"
}

Where “2” is an ID of a parent guide; “1310” is an ID of a child tour that has been created earlier, and we want to add to the guide; “child” indicates that we want to update the child item from the parent. Lastly,  “update” is a command that will update the relation and add a new item instead of replacing the old one.

advanced REST client body

We click the “SEND” button. The request is successful, so we add a new child item to the parent object.

parent user with child posts

Updating relation meta fields

You can also update the relation meta field via the REST API.

We add a new stroke to the code:

"meta": {"tour_start_date" : "2022-06-24", "_price_": "300"}

Where “tour_start_date” and “_price_” are relation text and date meta fields, and “2022-06-24” and “300” are values. You can update all types of meta fields that are available for relations.

updating meta fields via REST API

To check the results, hit the “Edit Meta” button.

edit meta button

As we can see, values have been saved via the REST API.

meta field values have been updated via REST API
Warning

Usually, the JetEngine plugin saves dates into the database in the YYYY-MM-DD format. Before updating you can check it by adding value to the date meta field on the website by clicking on the “Edit Meta” button. Then you will be sure in what format you should enter dates.

Save the meta data end enter the Endpoint URL into the search bar.

Here you can see in what format the date is saved. So, when updating or replacing the relation items, enter the value in the same format.

endpoint in the search bar

Replacing relation items

If you want to replace all related items with a new one, you need to enter “replace” instead of “update”:

"store_items_type": "replace",
replacing related items

A new related item has replaced the others.

new related item replaced others

Updating the parent item from the child

Now, we want to add a new guide to the tour; in other words, update the parent items (users) for the child object (post).

child post

JSON code is similar to the one that we used for adding a child item to the parent object, but you need to change the context:

{
  "parent_id": 3,
  "child_id": 710,
  "context": "parent",
  "store_items_type": "update",
  "meta": {"tour_start_date" : "2022-08-13", "_price_": "400"}
}
updating parent item from a child one

Parent user with the “3” ID has been added to the post with the “710” ID.

added parent item to the child object

Adding several children to the parent or vice versa

To add several children to the parent, you need to put IDs into the array:

"child_id": [710, 1174, 653, 1221, 649],
adding several children to the parent

We click the “SEND” button and proceed to the parent page.

New children items have been successfully added to the parent object.

children items have been added to the parent object

Also, you can add several parents to one child, putting several parent IDs to the array.

It’s also possible to connect several parents and children, so each parent in the code will have five children.

connecting several parents and several children

That’s all. Now you know how to get the relation data list and update or replace the children or parent items via REST API with the JetEngine WordPress plugin.

Was this article helpful?
YesNo

Need help?

Ask Facebook community

Get answers from 22K Crocoblock Community experts from all over the world.

Start a live chat

Ask question to support agent and share its details via chat messages.