diff --git a/core/server/models/plugins/filter.js b/core/server/models/plugins/filter.js index 657161ee9a..3f56014da1 100644 --- a/core/server/models/plugins/filter.js +++ b/core/server/models/plugins/filter.js @@ -139,15 +139,6 @@ filter = function filter(Bookshelf) { .query('leftOuterJoin', 'posts_authors', 'posts_authors.post_id', '=', 'posts.id') .query('leftOuterJoin', 'users as authors', 'posts_authors.author_id', '=', 'authors.id'); - // The order override should ONLY happen if we are doing an "IN" query - // TODO move the order handling to the query building that is currently inside pagination - // TODO make the order handling in pagination handle orderByRaw - // TODO extend this handling to all joins - if (gql.json.findStatement(this._filters.statements, {prop: /^authors/, op: 'IN'})) { - // TODO make this count the number of MATCHING authors, not just the number of authors - this.query('orderByRaw', 'count(authors.id) DESC'); - } - // We need to add a group by to counter the double left outer join // TODO improve on the group by handling options.groups = options.groups || []; diff --git a/core/server/models/post.js b/core/server/models/post.js index 40c7f25477..bc5b9a8faf 100644 --- a/core/server/models/post.js +++ b/core/server/models/post.js @@ -590,6 +590,11 @@ Post = ghostBookshelf.Model.extend({ order = `count(tags.id) DESC, ${order}`; } + // CASE: if the filter contains an `IN` operator, we should return the posts first, which match both authors + if (options.filter && options.filter.match(/(authors|author):\s?\[.*\]/)) { + order = `count(authors.id) DESC, ${order}`; + } + return order; }, diff --git a/core/test/unit/models/post_spec.js b/core/test/unit/models/post_spec.js index a774e8c56b..6c46a71830 100644 --- a/core/test/unit/models/post_spec.js +++ b/core/test/unit/models/post_spec.js @@ -86,7 +86,7 @@ describe('Unit: models/post', function () { withRelated: ['authors', 'tags'] }).then(() => { queries.length.should.eql(2); - queries[0].sql.should.eql('select count(distinct posts.id) as aggregate from `posts` left outer join `posts_tags` on `posts_tags`.`post_id` = `posts`.`id` left outer join `tags` on `posts_tags`.`tag_id` = `tags`.`id` left outer join `posts_authors` on `posts_authors`.`post_id` = `posts`.`id` left outer join `users` as `authors` on `posts_authors`.`author_id` = `authors`.`id` where (`posts`.`page` = ? and `posts`.`status` = ?) and (`authors`.`slug` in (?, ?) and (`tags`.`slug` = ? or `posts`.`feature_image` is not null)) order by count(authors.id) DESC'); + queries[0].sql.should.eql('select count(distinct posts.id) as aggregate from `posts` left outer join `posts_tags` on `posts_tags`.`post_id` = `posts`.`id` left outer join `tags` on `posts_tags`.`tag_id` = `tags`.`id` left outer join `posts_authors` on `posts_authors`.`post_id` = `posts`.`id` left outer join `users` as `authors` on `posts_authors`.`author_id` = `authors`.`id` where (`posts`.`page` = ? and `posts`.`status` = ?) and (`authors`.`slug` in (?, ?) and (`tags`.`slug` = ? or `posts`.`feature_image` is not null))'); queries[0].bindings.should.eql([ false, 'published',