Query blog posts

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.

Query a single blog post

When querying the live version of a specific blog post by the ID of its master version, you must perform the following:

  1. Get the master version.
    First, get an instance of the master version with the specified ID.
  2. Get the live version.
    The instance that corresponds to the ID argument is the master version of the blog post. You must get the live version explicitly.

    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.

  3. Return the live version. 

The following code queries the live version of a blog post by the ID of its master version:

Native API

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.

Fluent API

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.

Query all blog posts

When querying all blog posts, you must perform the following:

  1. Query all blog posts.
    First, get a query of all available blog posts. The query includes all live, master and temp versions. 
  2. Filter the query.
    Filter the query to return only the live versions. 
  3. Get the blog posts.
    Get the filtered items in a list. 
  4. Return the blog posts.
    Return the list of blog posts. 

The following code queries all blog posts:

Native API

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.

Fluent API

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.

To get the blog posts from a specific blog, use the following code:

In this code example you use the singular facade for the specified blog and get the live versions of its blog posts.

Was this article helpful?

Next article

Modify blog posts