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 use post type? #165

Closed e-ticaretim closed 5 years ago

e-ticaretim commented 5 years ago

How to use post type. I'm creating Post, but post type assigns it as post by default?

Frattie commented 5 years ago

I'm experiencing the same problems. If I try to generate a post with a post type other than "post", the generated post will have the "post" type anyway. Same goes if I try to update a post to another post type.

polushinmk commented 5 years ago

Hi all! To create post of custom post type you should do 2 things

  1. Enable rest api support for custom post type registering-a-custom-post-type-with-rest-api-support
  2. This is a one of contrintuitive things in WP REST API, but for creating post with custom type you should send requests to custom endpoint. For this task you can use our Custom Requests feature Example:
    var post = new Post()
    {
       Title = new Title("Title 1"),
       Content = new Content("Content PostCreate"),
       Type = "portfolio" //you custom post type
    };
    //change portfolio to your custom post type
    var createdPost = await _clientAuth.CustomRequest.Create<Post, Post>("wp/v2/portfolio",post); 

    @ThomasPe may be we can add a wrapper for this scenario. Also I added a wiki page https://github.com/wp-net/WordPressPCL/wiki/Create-post-with-custom-type

Frattie commented 5 years ago

Thanks @polushinmk ! It worked like a charm. As a help for those who use this library in vb.net and 'll come here seeking for help, here's the correct vb.net syntax : Dim createPost = Await client.CustomRequest.Create(Of Post, Post)("wp/v2/portfolio", newPost)