Magento 2: Category and product collection

Here I am writing code for category and product collection in Magento 2. Also, you can check how load product or category, get children products of the configurable product. You can see all the query related to product and category.

Category collection:

<?php
$objectManagerr = \Magento\Framework\App\ObjectManager::getInstance();
$categoryFactory = $objectManagerr->create('Magento\Catalog\Model\ResourceModel\Category\CollectionFactory');

$categories = $categoryFactory->create()							  
			->addAttributeToSelect('*');

foreach ($categories as $category):					
	echo 'Category name  =  '.$category->getName().'<br>';
endforeach; 
?>

Category load:

<?php
$categoryId = '10';

$_objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$object_manager = $_objectManager->create('Magento\Catalog\Model\Category')
->load($categoryId);

$categoryUrl = $object_manager->getUrl();
print_r($object_manager);
?>

Product collection:

<?php
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();

$productCollection = $objectManager->create('Magento\Catalog\Model\ResourceModel\Product\CollectionFactory');

$collection = $productCollection->create()
            ->addAttributeToSelect('*')
            ->load();

foreach ($collection as $product){
     echo 'Product name  =  '.$product->getName().'<br>';
}  
?>

Product load:

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$product = $objectManager->get('Magento\Catalog\Model\Product')->load(5);
?>

Get child products of configurable product:

<?php
$_product = Load product here;
$objectManagerr = \Magento\Framework\App\ObjectManager::getInstance();
$manager = $objectManagerr->get('Magento\ConfigurableProduct\Model\Product\Type\Configurable'); 
$childProducts = $manager->getUsedProducts($_product,null);   
                   
foreach($childProducts as $childProduct){   
	echo "ID##".$childProduct->getId();
	echo "Name##".$childProduct->getName();
}
?>

Enjoy!!!