Skip to content
This repository has been archived by the owner on Feb 3, 2021. It is now read-only.

Commit

Permalink
Fix bug where tags view wasn't sorting posts by date
Browse files Browse the repository at this point in the history
  • Loading branch information
claviska committed May 10, 2017
1 parent c48b186 commit 2c6b7a3
Showing 1 changed file with 43 additions and 33 deletions.
76 changes: 43 additions & 33 deletions source/controllers/theme/tag_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,41 +39,51 @@ module.exports = {
throw new Error('Page Not Found');
}
})
// Fetch posts with this tag
.then(() => models.postTags.findAndCountAll({
distinct: true,
// Fetch IDs for all posts with this tag
.then(() => models.postTags.findAll({
attributes: ['postId'],
where: {
tagId: tag.id
},
include: [
{
model: models.post,
where: {
status: 'published',
isPage: 0,
publishedAt: { $lt: Moment().utc().toDate() }
},
include: [
{
model: models.user,
as: 'author',
attributes: { exclude: ['password', 'resetToken'] }
},
{
model: models.tag,
through: { attributes: [] }, // exclude postTags
where: null // also return posts that don't have tags
}
]
}
],
limit: limit,
offset: offset,
order: [
sequelize.literal('`post.isSticky` DESC'),
sequelize.literal('`post.publishedAt` DESC')
]
}
}))
// Fetch posts that have any of those IDs
.then((matches) => {
let ids = [];

// Convert matches to an array of post IDs
if(matches) {
matches.forEach((row) => ids.push(row.postId));
}

// Return matching posts
return models.post.findAndCountAll({
distinct: true,
where: {
id: { $in: ids },
status: 'published',
isPage: 0,
publishedAt: { $lt: Moment().utc().toDate() }
},
include: [
{
model: models.user,
as: 'author',
attributes: { exclude: ['password', 'resetToken'] }
},
{
model: models.tag,
through: { attributes: [] }, // exclude postTags
where: null // also return posts that don't have tags
}
],
limit: limit,
offset: offset,
order: [
['isSticky', 'DESC'],
['publishedAt', 'DESC']
]
});
})
// Render the view
.then((posts) => {
if(page > 1 && !posts.rows.length) {
Expand All @@ -93,7 +103,7 @@ module.exports = {
// Render the template
res.render('tag', {
tag: tag,
posts: posts.rows.map((val) => val.post),
posts: posts.rows,
pagination: pagination,
meta: {
title: metaTitle,
Expand Down

0 comments on commit 2c6b7a3

Please # to comment.