---
title: "How to Assign Product Position for a Specific Category in Magento 2?"
url: "https://magecomp.com/blog/assign-product-position-for-a-specific-category-magento-2/"
date: "2022-11-25T12:50:50+00:00"
modified: "2025-02-17T06:18:51+00:00"
author:
  name: "Dhiren Vasoya"
  url: "https://magecomp.com/"
categories:
  - "How To"
  - "Magento 2"
word_count: 350
reading_time: "2 min read"
summary: "Hello Magento Friends,"
description: "You can assign a position to products for a specific category in Magento 2. Check how you can accomplish it."
keywords: "How To, Magento 2"
language: "en"
schema_type: "Article"
related_posts:
  - title: "How to Create Categories Tree Structure in Magento Programmatically?"
    url: "https://magecomp.com/blog/create-categories-tree-structure-magento-programmatically/"
  - title: "How to Auto Select First Child Product of Configurable Product in Magento 2?"
    url: "https://magecomp.com/blog/auto-select-first-child-product-of-configurable-product-magento-2/"
  - title: "Magento 2: How to Use Dynamic Row with UI Components"
    url: "https://magecomp.com/blog/use-dynamic-row-with-ui-components-magento-2/"
---

# How to Assign Product Position for a Specific Category in Magento 2?

_Published: November 25, 2022_  
_Author: Dhiren Vasoya_  

![How to Assign Product Position for a Specific Category in Magento 2](https://magecomp.com/blog/wp-content/uploads/2022/11/How-to-Assign-Product-Position-for-a-Specific-Category-in-Magento-2-1024x512.webp)

**Hello Magento Friends,**

In this blog, we will learn about How to Assign Product Position for a Specific Category in Magento 2?
[![Magento version upgrade](https://magecomp.com/blog/wp-content/uploads/2025/02/Magento-2-Version-Upgrade-CTA-1.webp)](https://magecomp.com/services/magento-upgrade-services/)

As you know, a category is a collection of products of a similar type. The admin can determine how the products can be displayed on the category page. In Magento 2, if you want to change the position of products for a specific category, you can accomplish it using the below-given steps.
## Steps to Assign Product Position for a Specific Category in Magento 2:
**Step 1:** Create a file in your Magento root directory and add the code as given below
```
<?php

use MagentoFrameworkAppBootstrap;
require __DIR__ . '/app/bootstrap.php';

$bootstraps = Bootstrap::create(BP, $_SERVER);
$object_Manager = $bootstraps->getObjectManager();

$app_state = $object_Manager->get('MagentoFrameworkAppState');
$app_state->setAreaCode('frontend');

try
{
    $bootstrap = MagentoFrameworkAppBootstrap::create(BP, $_SERVER);
    $objectManager = $bootstrap->getObjectManager();
    $appState = $objectManager->get('MagentoFrameworkAppState');
    $appState->setAreaCode('frontend');

    $objectManager = MagentoFrameworkAppObjectManager::getInstance();
    $categoryId = 16;
    $category = $objectManager->get('MagentoCatalogModelCategoryFactory')->create()->load($categoryId);
    $products = $category->getProductsPosition();

    // change position for specific product :
    $productId = 72;
    $newposition = 45;

    foreach($products as $id=>$value)
    {
            if($id == $productId)
            {
                $products[$productId] = $newposition;
            }
    }

    // change position for all products
    $newposition = 45;
    foreach($products as $id=>$value)
    {
                $products[$id] = $newposition;
    }

    $category->setPostedProducts($products);
    $category->save();

}
catch(Exception $e)
{
    print_r($e->getMessage());
}
```

[![Magento 2 Speed & Performance Optimization Services](https://magecomp.com/blog/wp-content/uploads/2025/02/Magento-2-Speed-Performance-Optimization-Services-CTA-1.webp)](https://magecomp.com/magento-performance-optimization.html)

**Output:**

![product position](https://magecomp.com/blog/wp-content/uploads/2022/11/product-position.png)

Using this, you can assign positions to a category using a specific product id. You can also assign positions to more than one product with this code. Based on your requirement, you can assign value for batch products or collections.
## Conclusion:
This way, you can Assign Product Positions for a Specific Category in Magento 2. Check out [**How to Load Products by Product ID and SKU in Magento 2**](https://magecomp.com/blog/load-products-by-id-and-sku-in-magento-2/). If you face any hardship in assigning the product position programmatically, you can freely ask me through the comment part. Share the article with your Magento friends and hold on with us to not miss out on our latest tutorials.
[![hyva theme development](https://magecomp.com/blog/wp-content/uploads/2025/02/hyva-theme-development-CTA-5.webp)](https://magecomp.com/services/hyva-theme-development/)

**Happy Coding!**


---

_View the original post at: [https://magecomp.com/blog/assign-product-position-for-a-specific-category-magento-2/](https://magecomp.com/blog/assign-product-position-for-a-specific-category-magento-2/)_  
_Served as markdown by [Third Audience](https://github.com/third-audience) v3.5.3_  
_Generated: 2026-10-18 17:44:53 UTC_  
