You need to use collections for that:
$messages = $objectManager->create('Custom\Module\Model\ResourceModel\Messages\Collection')->addFieldToFilter('posts_id', $this->getRequest()->getParam('id'));
This is assuming you have created a collection resource model for your Messages
entity.
NB: please try to avoid using the Object Manager directlytry to avoid using the Object Manager directly
Instead you can inject the collection class in your constructor:
protected $_messagesCollection;
public function __construct(
...
\Custom\Module\Model\ResourceModel\Messages\Collection $messagesCollection,
...)
{
...
$this->_messagesCollection = $messagesCollection;
}
And then you can use your variable directly in your code:
$messages = $this->_messagesCollection->addFieldToFilter('posts_id', $this->getRequest()->getParam('id'));