I want to sort a repository by more than one attribute. Any ideas of how to achieve this?
What I have so far is this:
public function getLatestPosts()
{
return $this->postRepository
->getList($this->buildSearchCriteriaForLatestPosts())
->getItems();
}
private function buildSearchCriteriaForLatestPosts()
{
$this->prepareSearchCriteriaBuilder();
$this->searchCriteriaBuilder->setPageSize(1);
return $this->searchCriteriaBuilder->create();
}
private function prepareSearchCriteriaBuilder()
{
/** @var \Magento\Framework\Api\SortOrder $publishDateOrder */
$publishDateOrder = $this->sortOrderBuilder
->setField(PostInterface::PUBLISH_DATE)
->setDirection(SortOrder::SORT_DESC)
->create();
/** @var \Magento\Framework\Api\SortOrder $isFeaturedOrder */
$isFeaturedOrder = $this->sortOrderBuilder
->setField('is_featured')
->setDirection(SortOrder::SORT_DESC)
->create();
$this->searchCriteriaBuilder->setSortOrders([$publishDateOrder, $isFeaturedOrder]);
}
I dont know why it is not working properly; what I want to achieve is to get the list of Posts sorted by is_featured and created_at in DESC. So even if somehow 2 or more posts are featured, it will display the latest one at the top
Any help will be appreciated! Thanks