wp-net / WordPressPCL

This is a portable library for consuimg the WordPress REST-API in (almost) any C# application
MIT License
335 stars 131 forks source link

Update custom fields on post? #344

Closed dburk2 closed 3 months ago

dburk2 commented 11 months ago

Is there a way to send an additional custom field value (other than Content, Excerpt, etc.) using the library via API? thanks

simonhammes commented 3 months ago

I just figured this out.

The following snippet seems to work:

var post = new Post
{
    Id = 18,
    Meta = new Dictionary<string, string>
    {
        ["my-custom-key"] = "some value"
    },
};

await client.Posts.UpdateAsync(post);

Please note that all meta keys need to be registered using register_post_meta() before you can use them, e.g.

register_post_meta('post', 'my-custom-key', [
    'type' => 'string',
    'single' => true,
    'show_in_rest' => true,
]);
ThomasPe commented 3 months ago

this would be a good addition to the docs as well

simonhammes commented 3 months ago

this would be a good addition to the docs as well

I will create a PR for that.