---
title: "How to Move Category Position Programmatically in Magento 2?"
url: "https://magecomp.com/blog/move-category-position-programmatically-magento-2/"
date: "2023-04-28T17:51:46+00:00"
modified: "2025-02-13T12:33:02+00:00"
author:
  name: "Dhiren Vasoya"
  url: "https://magecomp.com/"
categories:
  - "How To"
  - "Magento 2"
word_count: 318
reading_time: "2 min read"
summary: "Hello Magento Friends,"
description: "To change the category position from one parent category to another, you can accomplish it programmatically in Magento 2. Learn how"
keywords: "How To, Magento 2"
language: "en"
schema_type: "Article"
related_posts:
  - title: "How to Delete an Existing Foreign Key in Magento 2 Installer?"
    url: "https://magecomp.com/blog/delete-existing-foreign-key-magento-2-installer/"
  - title: "How to Disable the PageBuilder Module for a Specific Field in Magento 2"
    url: "https://magecomp.com/blog/disable-pagebuilder-module-for-specific-field-magento-2/"
  - title: "How to Change My Account Navigation Links Title Dynamically in Magento 2?"
    url: "https://magecomp.com/blog/change-account-navigation-links-title-dynamically-magento-2/"
---

# How to Move Category Position Programmatically in Magento 2?

_Published: April 28, 2023_  
_Author: Dhiren Vasoya_  

![How to Move Category Position Programmatically in Magento 2](https://magecomp.com/blog/wp-content/uploads/2023/04/How-to-Move-Category-Position-Programmatically-in-Magento-2-1024x512.webp)

**Hello Magento Friends,**

In today’s instructional guide, I will explain How to Move the Category Position Programmatically in Magento 2?
[![Hire Magento Programmer](https://magecomp.com/blog/wp-content/uploads/2025/02/Hire-Magento-Programmer-CTA-1.webp)](https://magecomp.com/services/hire-magento-programmer/)

A Magento 2 store contains various categories to classify the products based on their type. The category comprises various subcategories and products. You can quickly move subcategories in Magento 2 by drag-and-drop from the Admin Panel. Another way to move the category position is by executing it programmatically.
Let’s see how to move the category position from one parent category to another parent category along with its products programmatically in Magento 2.
## Steps to Move Category Position Programmatically in Magento 2:
**Category position before running the script**

[![category position - before](https://magecomp.com/blog/wp-content/uploads/2023/04/category-position-before-1.png)](https://magecomp.com/blog/wp-content/uploads/2023/04/category-position-before-1.png)

**Step 1:** Create a file in your Magento root directory at the below path.
**magento_root_directory\Changecategoryposition.php**

Now add the code as follows
```
<?php
use MagentoFrameworkAppInterface;
try
{
    require_once __DIR__ . '/app/bootstrap.php';
}
catch (Exception $e)
{
    echo 'Autoload error: ' . $e->getMessage();
    exit(1);
}
try
{
	$bootstrap = MagentoFrameworkAppBootstrap::create(BP, $_SERVER);
	$objectManager = $bootstrap->getObjectManager();
	$appState = $objectManager->get('MagentoFrameworkAppState');
	$appState->setAreaCode('frontend');

       $category = $objectManager->get('MagentoCatalogApiCategoryManagementInterface');


	$categoryId = 5; //id of The category to be moved
        $parentId = 6; //id of parent category where the new category to move
        $afterId = 7; //id of category after which you want to move

        $CategoryMoveSuccess = $category->move($categoryId, $parentId, $afterId);

        if($CategoryMoveSuccess)
        {
            echo "Successfully Move Category Position";
        }
        else
        {
            echo "Not Move Category Position";
        }
}
catch(Exception $e)
{
	echo "Error : ".$e->getMessage();
}

?>
```

[![Magento version upgrade](https://magecomp.com/blog/wp-content/uploads/2025/02/Magento-2-Version-Upgrade-CTA-2-2.webp)](https://magecomp.com/services/magento-upgrade-services/)

**Step 2:** After the above step, you will need to run the below-given URL.
**https://yourdomain.com/Changecategoryposition.php**

**Category position after running the script**

[![category position - after](https://magecomp.com/blog/wp-content/uploads/2023/04/category-position-after-1-300x235.webp)](https://magecomp.com/blog/wp-content/uploads/2023/04/category-position-after-1.png)

## Conclusion:
This way, you can change the category position in Magento 2 programmatically. Alternatively, you can integrate [**Duplicate Categories Extension for Magento 2**](https://magecomp.com/magento-2-duplicate-categories.html) to create a copy of the category.
Share your doubts with me in the comment box. Stay in touch with us for more Magento 2 solutions.
[![Magento Malware Removal](https://magecomp.com/blog/wp-content/uploads/2025/02/Magento-Malware-Removal-CTA-6.webp)](https://magecomp.com/magento-malware-removal.html)

**Happy Coding!**


---

_View the original post at: [https://magecomp.com/blog/move-category-position-programmatically-magento-2/](https://magecomp.com/blog/move-category-position-programmatically-magento-2/)_  
_Served as markdown by [Third Audience](https://github.com/third-audience) v3.5.3_  
_Generated: 2026-10-29 21:35:18 UTC_  
