Skip to content

PostsHandler

Marco Siccardi edited this page Mar 16, 2018 · 5 revisions

To get a new instance, either create an inline variable or use your favorite Locator:

//inline var
var handler = new PostsHandler();

//MVVMLight (or any other Locator):
SimpleIoc.Default.Register<IPostsHandler>(()=> new PostsHandler());

If you want to add the "If-Modified-Since"-Header and "User-Agent"-Header, just fill in the parameters:

//inline var
var handler = new PostsHandler(DateTime.Now, "MyAwesomeApp", "1.0.0");

//MVVMLight (or any other Locator):
SimpleIoc.Default.Register<IPostsHandler>(()=> new PostsHandler(DateTime.Now, "MyAwesomeApp", "1.0.0"));

Getting Posts

There are several ways to get and filter posts from the WordPress API:

getting a list of posts with default parameters:

var posts = handler.GetPostsAsync("yourBaseUrl");

Note that you have optional parameters for pages, page counts, and order of the results.

get a single post by its id:

var post = handler.GetPostAsync("yourBaseUrl", 12345);

get a single post by its slug

var post = handler.GetPostBySlugAsync("yourBaseUrl", "slug");

get a list of posts with tag ids:

(to get the tag ids, use the TagHandler or extract them from other objects)

var posts = handler.GetPostsByTagsAsync("yourBaseUrl", { 4567, 4568 });

search for posts:

var posts = handler.SearchPostsAsync("yourBaseUrl", "your search terms");

Note that you have optional parameters for pages, page counts, and order of the results.

All above calls will result in a WordPressEntitySet<TWordPressEntity> or a WordPressEntity<TWordPressEntity>. If there was an error on the API side, the Error property will be filled, otherwise it will be null and the Value will hold the result.