/**
  * prepare the collection
  * @return $this
  */
 protected function _prepareCollection()
 {
     /** @var \Mageplaza\Blog\Model\ResourceModel\Post\Collection $collection */
     $collection = $this->postCollectionFactory->create();
     if ($this->getTopic()->getId()) {
         $constraint = 'related.topic_id=' . $this->getTopic()->getId();
     } else {
         $constraint = 'related.topic_id=0';
     }
     $collection->getSelect()->joinLeft(['related' => $collection->getTable('mageplaza_blog_post_topic')], 'related.post_id=main_table.post_id AND ' . $constraint, ['position']);
     $this->setCollection($collection);
     parent::_prepareCollection();
     return $this;
 }
 /**
  * execute action
  *
  * @return \Magento\Backend\Model\View\Result\Redirect
  */
 public function execute()
 {
     $collection = $this->filter->getCollection($this->collectionFactory->create());
     $delete = 0;
     foreach ($collection as $item) {
         /** @var \Mageplaza\Blog\Model\Post $item */
         $item->delete();
         $delete++;
     }
     $this->messageManager->addSuccess(__('A total of %1 record(s) have been deleted.', $delete));
     /** @var \Magento\Backend\Model\View\Result\Redirect $resultRedirect */
     $resultRedirect = $this->resultFactory->create(\Magento\Framework\Controller\ResultFactory::TYPE_REDIRECT);
     return $resultRedirect->setPath('*/*/');
 }
 /**
  * @return \Mageplaza\Blog\Model\ResourceModel\Post\Collection
  */
 public function getSelectedPostsCollection()
 {
     if (is_null($this->postCollection)) {
         $collection = $this->postCollectionFactory->create();
         $collection->join('mageplaza_blog_post_topic', 'main_table.post_id=mageplaza_blog_post_topic.post_id AND mageplaza_blog_post_topic.topic_id=' . $this->getId(), ['position']);
         $this->postCollection = $collection;
     }
     return $this->postCollection;
 }