How to apply custom condition before product collection loaded magento 2?

You can apply custom conditions to Product Collection before load by creating a plugin in Magento 2. Some situation in your project need to add some conditions before product collection loaded you can achieve it by below way,

In Magento\Catalog\Model\ResourceModel\Product\Collection file load() method is used for load the product collection globally.

For Apply custom condition before product collection loaded, you need to create a di.xml file for plugin definition and plugin PHP file for add your custom logic.

Under the di.xml file, You need to add plugin definition to create plugin beforeLoad( ) method.

Let’s create a simple module, Rbj_Product where Rbj is Vendor name and Product is the module name.
Our Module resides under app/code/Rbj/Product location.

Path:  app/code/Rbj/Product/registration.php

<?php
\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'Rbj_Product',
    __DIR__
);

Path: app/code/Rbj/Product/etc/module.xml file,

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Rbj_Product" setup_version="1.0.0">
        <sequence>
            <module name="Magento_Catalog" />
        </sequence>
    </module>
</config>

Path: app/code/Rbj/Product/etc/frontend/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\ResourceModel\Product\Collection">
        <plugin name="beforeproductCollectionPlugin" type="Rbj\Product\Plugin\Catalog\Model\ResourceModel\Product\CollectionPlugin" />
    </type>
</config>

You need to create a plugin file under the below location,

Path : Rbj\Product\Plugin\Catalog\Model\ResourceModel\Product\CollectionPlugin.php

<?php
namespace Rbj\Product\Plugin\Catalog\Model\ResourceModel\Product;

class CollectionPlugin
{
    /**
     * @param Collection $subject
     * @param bool $printQuery
     * @param bool $logQuery
     */
    public function beforeLoad(\Magento\Catalog\Model\ResourceModel\Product\Collection $subject, $printQuery = false, $logQuery = false)
    {
        if (!$subject->isLoaded()) {
            // you can do your customzation/custom logic with $subject object
        }

        return [$printQuery, $logQuery];
    }
}