How to create order status and state programmatically in Magento 2?

You can create a custom order state and order status programmatically with the help of the class Magento\Sales\Model\Order\Status.

I have just created a new order status called processing_review and assigned the status to the processing state.

You need to create Datapatch to install the new status in Magento 2.
Just create Datapatch in your module setup folder,

Path: app/code/Rbj/OrderProcessingReview/Setup/Patch/Data/AddOrderStatus.php

<?php

declare(strict_types=1);

namespace Rbj\OrderProcessingReview\Setup\Patch\Data;

use Magento\Framework\Setup\Patch\DataPatchInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Sales\Model\Order\StatusFactory;
use Magento\Sales\Model\ResourceModel\Order\StatusFactory as StatusResourceFactory;

class AddOrderExportStatus implements DataPatchInterface
{
    public function __construct(
        private readonly ModuleDataSetupInterface $moduleDataSetup,
        private readonly StatusFactory            $statusFactory,
        private readonly StatusResourceFactory    $statusResourceFactory
    ) {
    }

    /**
     * Add New Order export status and state.
     * @return void
     * @throws \Exception
     */
    public function apply(): void
    {
        $this->moduleDataSetup->startSetup();

        $statusResource = $this->statusResourceFactory->create();
        $status = $this->statusFactory->create();

        try {
            $statusResource->save($status);

            $status->setData([
                'status' => 'processing_review',
                'label' => 'Order Review internally'
            ]);
            $status->assignState('processing', true, true);
        } catch (\Exception $exception) {
            throw new $exception;
        }

        $this->moduleDataSetup->endSetup();
    }

    /**
     * {@inheritdoc}
     */
    public static function getDependencies()
    {
        return [];
    }

    /**
     * {@inheritdoc}
     */
    public function getAliases()
    {
        return [];
    }
}

Once you run the setup upgrade command, You will able to see the new order status from the admin panel.

Click on Stores -> Settings -> Order Status

With the help of the above steps, you can easily create an order Status Programmatically in Magento 2.