You can do this by following code,
<?php
namespace Vendor\Module\Block;
class Test extends \Magento\Framework\View\Element\Template
{
protected $_productCollectionFactory;
protected $_session;
protected $_customerFactory;
public function __construct(
\Magento\Backend\Block\Template\Context $context,
\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory,
\Magento\Customer\Model\CustomerFactory $customerFactory,
\Magento\Customer\Model\Session $session,
array $data = []
) {
$this->_productCollectionFactory = $productCollectionFactory;
$this->_customerFactory = $customerFactory;
$this->_session = $session;
parent::__construct($context, $data);
}
public function getCustomerId()
{
if ($this->_session->isLoggedIn()) {
return $this->_session->getCustomer()->getId();
}
}
public function getProductCollection()
{
$userId = $this->getCustomerId();
if (!empty($userId)) {
/*$customerCollection = $this->_customerFactory->create()
->getCollection()
->addAttributeToSelect("*")
->load(); */
$productCollection = $this->_productCollectionFactory->create();
$productCollection->addAttributeToSelect('*')
->addAttributeToFilter(
'user_id',
array('eq' => $userId)
); // user_id is your attribute name
return $productCollection;
}
}
}
Add view/frontend/layout/catalog_category_view.xml
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceContainer name="category.products.list">
<block class="Namespace\Modulename\Block\Test" name="my.block.name" template="Namespace_Modulename::product/list.phtml" />
</referenceContainer>
</body>
</page>
copy this list.phtml
file from
vendor/magento/module-catalog/view/frontend/templates/product/list.phtml
to
view/frontend/templates/product/list.phtml