Hexo: Advanced get_posts query

Created on 6 Jan 2015  ·  7Comments  ·  Source: hexojs/hexo

Is it possible to use the get_posts helper function to return all posts that are not in a category? I've attempted to use:
get_posts({count: 14, orderby: "date", order: -1, query:{categories: 'Diary', operator: 'not'}}) and
get_posts({count: 14, orderby: "date", order: -1, query:{categories: 'Diary', operator: '!='}}) or
get_posts({count: 14, orderby: "date", order: -1, query:{categories: 'Diary', operator: '<>'}})
but it returns no posts instead?

I've also noticed from pull #950 that the get_posts helper has been removed. Is it being replaced by anything else or will there be no way to get a subset of posts once this has gone?

discussion

Most helpful comment

@LouisBarranqueiro thanks for the tip, updated and now working:

<%site.categories.findOne({name: 'articles'}).posts.sort('date', -1).limit(14).each(function(post) {%>
  <%- partial('partials/entry-post', {post: post, index: true}) %>
<% })%>

All 7 comments

get_posts helper was removed in 3.0 since it's too complicated. You can try this:

site.categories.find({name: 'Diary'}).posts.sort('date', -1).limit(14)

Sorry for the delayed reply.

I've tried using the site.categories.find({name: 'Diary'}).posts.sort('date', -1).limit(14) on the site just now to see how it would work, but with version 2.8.3 the error returned is:
"Cannot call method 'sort' of undefined"

Would this code only work after upgrading to version 3, or am I doing something wrong?

In Hexo 2, you have to populate the posts first.

site.categories.find({name: 'Diary'}).populate('posts').posts.sort('date', -1).limit(14)

@tommy351 I followed the 2.x.x migration to 3.0 docs. Before updating I was using get_posts to display posts from certain categories. Here is a link of my theme in v2.8.3 with get_posts working https://github.com/ianrose/ianrose-source/blob/master/themes/ianrose-theme/layout/index.ejs#L13-L30

After upgrading to v3.0 I tried to use site.categories.find({name: 'notes'}).posts.sort('date', -1).limit(14) and am getting the error Cannot call method 'sort' of undefined

The v3.0 index.ejs with the offending code:
https://github.com/ianrose/ianrose-source/blob/hexo-3/themes/ianrose-theme/layout/index.ejs#L11

My latest package.json:
https://github.com/ianrose/ianrose-source/blob/hexo-3/package.json#L9

@ianrose
@nxfifteen
The function find doesn't work, you have to use findOne. It works fine on my project.
I opened an issue to report this : issue#1174

@LouisBarranqueiro thanks for the tip, updated and now working:

<%site.categories.findOne({name: 'articles'}).posts.sort('date', -1).limit(14).each(function(post) {%>
  <%- partial('partials/entry-post', {post: post, index: true}) %>
<% })%>

How can I search only posts publicated after a specific date? I tried to use "$gte" but it returns an error.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ghost picture ghost  ·  27Comments

JackieLuo picture JackieLuo  ·  45Comments

Xuanwo picture Xuanwo  ·  42Comments

fishercoder1534 picture fishercoder1534  ·  26Comments

LouisBarranqueiro picture LouisBarranqueiro  ·  28Comments