For this you can try below code.
Create plugin in di.xml file
app/code/[vendor]/[Module]/etc/di.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\Catalog\Model\Layer">
<plugin disabled="false" name="Vendor_Module_Plugin_Magento_Catalog_Model_Layer" sortOrder="10" type="[vendor]\[Module]\Plugin\Magento\Catalog\Model\Layer"/>
</type>
</config>
Now create Plugin PHP file
[vendor]\[Module]\Plugin\Magento\Catalog\Model\Layer.php
<?php
namespace [vendor]\[Module]\Plugin\Magento\Catalog\Model;
class Layer
{
public function aroundGetProductCollection(
\Magento\Catalog\Model\Layer $subject,
\Closure $proceed
) {
$collection = $proceed();
$collection
->addAttributeToSelect('*')
->addAttributeToFilter(array(
array(
'attribute' => 'discontinue',
'null' => true),
array(
'attribute' => 'discontinue',
'eq' => '0')
));
return $collection;
}
}
Note: Before this plugin please create discontinue product attribute and place your attribute code in plugin file