Getting Posts by Category with Hexo

While building with Hexo I came across a need to get all the posts by a specific category and render them through a specific partial.

Hexo is a great open source project in many ways. However due to language barriers the English documentation needs help from the project’s community. Myself included. Skip right to the solution.

The Scenario

Let’s say we have two categories, “Articles” and “Projects”. You have created three posts categorized as “Articles” and three posts categorized as “Projects”. On the homepage you want to display all the “Article” posts with a headline and excerpt text. Then in another column you want to display all the “Project” posts with a headline and a large image.

At first I could not find an example of this scenario anywhere, in the documentation, GitHub issues, or countless Google Searches.

The challenge it turned out was not the lack of functionality with Hexo it was parsing through the docs, repositories and using Google translate.

The Solution

Thanks to Brad Oyler for pairing up with me to figure this out. Hexo does offer helpers that leverage its API. The helper, get_posts is the one to use.

<% get_posts(
  count: 3,
  orderby: "updated",
  order: -1,
  query: categories: ‘projects’
  }).each(function(post)  %>
    <%- partial('_partial/index-post', post: post, index: true) %>
<% }) %>

The query option is the key to all this and knowing to pass categories: 'projects' did the trick.