wp-net / WordPressPCL

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

Get post, page or category by slug #102

Closed Marneus77 closed 6 years ago

Marneus77 commented 6 years ago

Which method allows me to get a post, category or page using slug instead of id? I can't find a way. e.g. /wp/v2/POSTS?slug=the_slug

aaldrich commented 6 years ago

@Marneus77 You can pull a list of posts like this. Not ideal but it works.

client.Posts.GetAll().Result.Where(x => x.Slug.ToLower() == the_slug)

Marneus77 commented 6 years ago

Ok thanks. I thought maybe with custom query? A GetBySlug would be nice also (like GetById). It will have to do.

ThomasPe commented 6 years ago

We should have this as an option in the query builder. Getting everything from the server first is too expensive.

polushinmk commented 6 years ago

For this purpose, it was necessary to create a lot of methods. Such as GetWithInclude, getWithExclude, GetByTags, GetBySearch and so on. We decided that this is not practical and provided you with the opportunity to create a query yourself using QueryBuilder. For your purposes, the following code is suitable.

var PostsQuery = new PostsQueryBuilder(){
        Slugs=new string[] { "the_slug1","the_slug2"}
};
var result = await client.Posts.Query(PostsQuery);
Marneus77 commented 6 years ago

Thank you, it's working