Skip Navigation

[Resolved] What does the json request look like to update custom field values?

This thread is resolved. Here is a description of the problem and solution.

Problem:

How to add or edit Toolset custom fields via REST API?

Solution:

Toolset custom fields use the native WordPress methods and you would do the same steps like you want to edit a native WordPress custom field.

The only difference is that in the native WordPress functions you should add the "wpcf-" prefix to the Toolset custom fields names.

Relevant Documentation:

https://toolset.com/forums/topic/what-does-the-json-request-look-like-to-update-custom-field-values/page/2/#post-2666487

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.

This topic contains 23 replies, has 3 voices.

Last updated by Christopher Amirian 1 year, 6 months ago.

Assisted by: Christopher Amirian.

Author
Posts
#2661109
Screenshot 2023-11-06 at 8.52.35 AM.png

Hi Christopher,

Here's a screen shot of my postman request and the response that I am getting from the custom institutions endpoint. Does this look correct?

#2661249

And the question I asked you previously, in Postman how do I connect to the environment you created? Im not sure what the purpose of creating that environment if I am unable to connect to it via Postman. I feel like this conversation is going in circles a bit and not coming to a conclusion.

#2661583

Christopher Amirian
Supporter

Languages: English (English )

Hi there,

I have an answer from the second tier support regarding the issue.

Unfortunately, those additional items you see which are formatted custom fields data are read only and you can not update them.

But please consider that Toolset uses standard post meta feature to add those custom fields.

So the RAW data is available for you with the native WordPress methods to retrieve the custom fields, like you would do with any native WordPress custom fields.

For simple fields such as text and stuff the Raw value and formatted values are not different and you can use the native WordPress methods to edit the values of those custom fields.

But in specific scenarios that the custom fields are more complicated, the raw data are stored in various methods such as PHP Serialized arrays and stuff.

To edit those, you will need to use the same raw format so that Toolset understands the value change,

Thanks.

#2662251

Can you provide us with an example of the format for a RAW data request? Also - is what you are describing above available through the REST API, or does this need to be in PHP on WordPress?

#2663603

Christopher Amirian
Supporter

Languages: English (English )

Hi there,

The raw data for for example a normal text field is same with the normal values.

An example of a Raw data that is not normal value is date which is using UNIX timestamp.

Or other complex fields that use serialized array. For more information:

hidden link.

We can not give you details on each field type and you need to check it yourself on the database on how they are stored.

The editing can be done via Rest API with standard WordPress features. Just think that Toolset is not installed. What would you do to retrieve and update a native custom field of WordPress? Do the same for Toolset fields.

Here you can learn more:

hidden link.

Thanks.

#2665961

We have tried every suggestion you have made including engaging a WP expert to do the same, and it simply isn't working. We need to escalate this to someone who can help us debug this and that can communicate clearly about it. How do we get in contact with the persons who are experts on this subject at Toolset? This is either a bug with toolset or a conflict with plug-ins but the latter seems unlikely. We need more from our paid support.

#2666487

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+01:00)

Screenshot 2023-11-20 at 09.41.04.png

Hi there

Christopher asked me to step in here and help.

The previous sandbox site has expired, so I created a new one to demonstrate what is involved to enable updating Toolset custom fields via REST API POST requests.

Most of what is involved is uses vanilla WordPress REST API concepts, but there are a couple of things particular to Toolset that I will highlight.

First, to re-iterate, the Toolset REST API integration relates only to retrieving custom fields (formatted), not for updating them. To update custom fields you will be using the regular WordPress REST API routes, working with raw field values (as stored in wp_postmeta).

One thing to be aware of is that Toolset custom fields are stored in wp_postmeta with a 'wpcf-' prefix, which you will need to use in the REST API, so if you had a custom field with a slug of 'priority' then its metakey would be 'wpcf-priority'.

When you create custom fields in Toolset they are not registered as being available in the REST API by default, and so you must update the settings for the fields you want to expose to the REST API via the WordPress function register_meta.

Use this link to log in to the new sandbox site: hidden link

I have registered a custom post type "Things", and added two custom fields to it: "choices" and "text-address".

Go to Toolset > Settings > Custom Code and you will see there is an active code snippet which updates the options for these two post types using the register_meta function to make them available in the REST API (and also to specify the fields would only have a single value).

Here's the code for the "choices" custom field:

// Register custom fields as available in REST API
register_meta(
    'post',
    'wpcf-choices', // meta key of Types custom fields includes a wpcf- prefix
    [ 'show_in_rest' => true,
      'single' => true
    ]
);

You can check the REST API is working (use whatever tool suites; I tested with the online test tool reqbin.com) with a simple GET request such as to this endpoint:

<em><u>hidden link</u></em>

That should return JSON data for the "Something" post (post ID 15).

To make a POST request to a similar route to update post data it is necessary to make authenticated requests.

For the purposes of testing I added a Basic Auth plugin to the site so that you can make requests with the username and password of an admin user. For testing I added such as user to this site: otgsuser / otgspass.

You can then make an authenticated POST request where the post body includes the data to be updated, like so:

{"meta": {
  "wpcf-choices": "2",
  "wpcf-text-address":"2 Main Street\nThe City\nNowhereland\n"
}}

That will update the "choices" field value to "2". Choices is a select custom field, where the option with value 2 has a label "Second choice".

The "text-address" field is a multiline text field.

You can see in the screenshot me making a request to update post 18 ("Nothing") to provide values for the custom fields, and the response shows that the post has been updated with those values.

The only point in the above that is specific to Toolset is the 'wpcf-' prefix used for custom field meta keys, but don't forget to update the custom field options to show_in_rest using the register_meta WordPress function.

#2666689
Screenshot 2023-11-20 at 5.35.23 PM.png
Screenshot 2023-11-20 at 5.35.02 PM.png
Screenshot 2023-11-20 at 5.27.29 PM.png
Screenshot 2023-11-20 at 5.26.29 PM.png
Screenshot 2023-11-20 at 5.25.48 PM.png

Hi Nigel,

Thank you for the response and the detailed information. Please see the attached screenshots (5 of them). I attached a screenshot from Postman that includes the request that you included above. I tried the request with a few other parameters and also as shown. I appended a "2" at the end of the City and College fields but as you can see the change did NOT save. I have also attached an image of the register_meta snippets that were added. Also screen shots of the fields with their slugs.

If we could arrange a Zoom meeting soon, would you be available to help us work through the issue we are having?

#2668245

Christopher Amirian
Supporter

Languages: English (English )

Hi there,

I will inform Nigel about your answer, but just for the initial reply, we do not have a Zoom support.

I'd appreciate it if you could replicate the issue that you have mentioned here:

hidden link

As it worked ok there. So it will help to know if there is an issue with the approach.

Thanks.

#2669753

Christopher/Nigel - thank you for your help, we were able to get this resolved based on the information Nigel provided. I compared your environment to ours and we ended up finding a checkbox within the post type related to the custom fields that needed to be checked. Once that was checked the integration started working. Thanks again for your help it was very much appreciated.