Croco Black Friday 2024 banner element
Copy discount code:
bfcroco2024
Days
Hours
Minutes
Croco Black Friday 2024 banner element
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.

Before you start, check the tutorial requirements:

  • JetEngine plugin installed and activated with a relation built

  • An application that works with REST API

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.

Things to know

In this tutorial, we work with the Advanced REST Client application, but you can use any application that works with REST API.

Public REST API for Relations Settings Overview

Navigate to the WordPress Dashboard > JetEngine > Relations tab and click the “Edit” button next to the needed relation.

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

list of relations

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

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

register get items rest api endpoint
  • 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.
Things to know

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, the “edit_pages” capability, only editors and admins can edit them.

Enable the Register update REST API Endpoint toggle.

register update items 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
}

In this piece of code, you need to enter the ID/IDs of the parent item/items, ID/IDs of the child item/items; the 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 inserts the new ones instead when updating, and “update” adds the new elements to the existing ones.

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

Update Relation Data via REST API

We will show how to update and replace related items using the Advanced REST Client application as an example.

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” Name and “application/json” Value. If you want, you can also add the “Authorization” Header.

headers tab of the advanced rest client

Updating the child item from the parent

We decided to add a new child tour to the parent guide. Now, the current user doesn’t have any related posts.

related posts not connected

In the Body tab, we leave the default “JSON” Body content type and “Raw input” Editor view.

In the following textarea, 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.

succesful update code

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

related post connected

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" : "2024-06-24", "_pricing": "300"}

Where “tour_start_date” and “_pricing” are “Text” and “Date” meta fields from the relation, and “2024-06-24” and “300” are values. You can update all types of meta fields that are available for relations.

Click the “Send the request” button.

meta update code

To check the results, hit the “Edit Meta” button next to the connected child post.

edit meta button

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

meta fields updated
Warning

The JetEngine plugin usually 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 the “Edit Meta” button. Then you will be sure in what format you should enter dates.

Save the meta data and 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 url

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",
succesful replace code

After the “Send the request” button is pressed, the new related item will replace the old ones.

connected child replaced

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).

post with related users not connected

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" : "2024-08-13", "_price_": "400"}
}
succesful parent update code

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

post with related user connected

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],
child ids array in the code

Click the “Send the request” button and proceed to the parent page.

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

several related posts connected to the user

You can also add several parents to one child by putting several parent IDs in the array.

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

parent ids array in the code

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.