wp-net / WordPressPCL

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

How to attach images to a post / custom post type? #191

Closed dirkil closed 3 years ago

dirkil commented 4 years ago

Hi there!

I guess I have a rather simple question but nevertheless I cannot get it to work.

I created a custom post type and now I want to:

I managed to upload an image by calling: client.Media.Create(path, name)

Therefore I would appreciate if someone can explain to me how I can attach an uploaded image to my custom post type and how to set an image as the featured image.

Many thanks in advance.

Regards, Dirk

ThomasPe commented 4 years ago

You should be able to set the ID of your uploaded file as FeaturedMedia on your post object. So something like this (untested code):

var media = await client.Media.Create(path, name);
var post = await client.Posts.Get(id);
post.FeaturedMedia = media.Id;
await client.Posts.Update(post);
ThomasPe commented 3 years ago

I’m closing this issue because it has been inactive for a while. Please feel free to reopen if you still encounter this issue. Thanks!