Open dirkil opened 4 years ago
Have you checked if it works when you're using the API directly (for example using Postman)?
Excellent advice! I tried that and found out that when I enter data for my custom post type fields through the browser (when I am logged in) the data end up in a place I could not have imagined:
"author": 1,
"featured_media": 0,
"menu_order": 0,
"comment_status": "open",
"ping_status": "open",
"template": "",
"format": "standard",
"meta": [],
"cmb2": {
"geo_metabox": {
"_psi_geo_plz": "12345",
"_psi_geo_ort": "",
"_psi_geo_strasse": "",
"_psi_geo_hausnr": "",
"_psi_geo_land": ""
}
I am referring to the field _psi_geo_plz for example. I would have thought it lives somewhere underneath "meta" but in fact in lives underneath "cmb2".
So, I rephrase my question: how can I set "cmb2.geo_metabox._psi_geo_plz" with WordpressPCL?
I found out that I have to issue a HTTP POST on the following URL to update the value:
Check out the CustomRequest method which allows you to specifiy the path & model you'd like to use: https://github.com/wp-net/WordPressPCL/wiki/CustomRequest
@ThomasPe Thank you for this great advice. I tried that but I still get an error and I would like you to have a look at that. It is probably something trivial but I am not able to figure it out myself.
Here is the C# code to update the field _psi_geo_plz:
`private static async Task RunSampleUpdate()
{
// JWT authentication
var client = new WordPressClient("https://
try
{
if (await client.IsValidJWToken())
{
var updatedPost = await client.CustomRequest.Update<Post, Post>("cmb2/v1/boxes/geo_metabox/fields/_psi_geo_plz?object_type=post&object_id=13784&value=55555", new Post());
}
}
catch (Exception e)
{
Console.WriteLine("Error:" + e.Message);
}
}
`
When I run this a JsonReaderException is thrown with the message: "Could not convert string to integer: _psi_geo_plz. Path 'id', line 1, position 610."
When I send the request following through Postman the value is updated in Wordpress. So, I reckon the Wordpress part is correct. It must be some problem in my C# code.
Here is the request I send through Postman:
curl --location --request POST 'https://<domain>/wp-json/cmb2/v1/boxes/geo_metabox/fields/_psi_geo_plz?object_type=post&object_id=13784&value=55555' \ --header 'Content-Type: application/json' \ --data-raw ''
I hope you can spot what I am doing wrong. Any help is very much appreciated!
Regards, Dirk.
I use WordpressPCL to create posts for my custom post type. This works fine so far. But I also would like to insert / update meta for it. I tried it like that:
This creates the post as a custom post type but no meta data is set whatsoever.
I also added this to my functions.php:
But no success. I can see only the debug entry "HEUREKA - Get!"; the update callback is not called.
Any help with this is very much appreciated.
Regards, Dirk.