This topic explains how to query instances of blog posts. The examples below show you how to query all of the available blog posts or how to query only a specific blog post by its ID.
NOTE: The code examples below work with the ID of the master version of the blog post and return the live version of the blog post. For more information about other scenarios, see For developers: Query master and live versions in For developers: Content lifecycle.
When querying the live version of a specific blog post by the ID of its master version, you must perform the following:
NOTE: The live version is used only when displaying the blog posts in a front-end scenario. If you want to manipulate the blog post, you must use the master version. To transfer the changes to the live version, publish the master version.
For more information, see For developers: Modify blog posts.
The following code queries the live version of a blog post by the ID of its master version:
First, you get an instance of the BlogsManager class. You get the specified blog post by querying all blog posts and filtering the collection by the ID of the blog post. If the item exists, you get its live version. If no live version exists (i.e. the item has not been published), you return null. Finally, you return the live version.
You can also use the following code to retrieve the blog post:
Note that when the blog post does not exist, an exception of type ItemNotFoundException is thrown.
First, you get the singular blog post facade of the master version with the specified ID. Then, to load the live version of the item, you call the GetLive method of the facade. Finally, to get the instance of the live version, you call the Get method.
When querying all blog posts, you must perform the following:
The following code queries all blog posts:
First, you get an instance of the BlogsManager class. You query all blog posts, including the live, master and temp versions of each. You filter the collection to return only the live versions. Finally, you return the blog posts as a list.
To get the blog posts from a specific blog, use the following code:
In this code example you additionally filter the collection by the ID of the parent blog.
First, you get an instance of the plural blog posts facade. Then you get all the blog posts, including the live, master and temp versions of each. You filter the collection to return only the live versions. Finally, you return the blog posts as a list. If no blog posts exist or there are not any published blog posts, you return an empty list.
In this code example you use the singular facade for the specified blog and get the live versions of its blog posts.
Back To Top