{"id":2105,"date":"2022-09-14T11:33:23","date_gmt":"2022-09-14T11:33:23","guid":{"rendered":"https:\/\/meetanshi.com\/blog\/programmatically-set-url-key-of-category-in-magento-2\/"},"modified":"2025-03-17T08:50:41","modified_gmt":"2025-03-17T08:50:41","slug":"programmatically-set-url-key-of-category-in-magento-2","status":"publish","type":"post","link":"https:\/\/meetanshi.com\/blog\/programmatically-set-url-key-of-category-in-magento-2\/","title":{"rendered":"How to Programmatically Set URL Key of Category in Magento 2"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">In this blog post, I will show you the complete method to <em><strong>programmatically set URL key of <a href=\"https:\/\/meetanshi.com\/blog\/how-to-solve-category-move-error-in-magento-2\/\">category in Magento 2<\/a><\/strong><\/em>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Categorizing the products into different categories is imperative for businesses with a wide range of products on their shelves. Magento 2 (Adobe Commerce) allows store owners to <a title=\"Create a New Root Category in Magento 2\" href=\"https:\/\/meetanshi.com\/blog\/create-new-root-category-in-magento-2\/\" target=\"_blank\" rel=\"noopener\">create different categories<\/a> and sub-categories for better organization of products in the stores.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The URL key or slug is the last part of the web address, which is easy to read and understand for the users. If you are a Magento 2 store owner, it is essential to make the URL key of the category pages SEO-friendly and optimized for a better user experience. By default, this can be done from the system configuration in the Magento backend (<a title=\"Catalog and Product URLs Magento 2\" href=\"https:\/\/experienceleague.adobe.com\/en\/docs\/commerce-admin\/catalog\/catalog\/catalog-urls\" target=\"_blank\" rel=\"noopener\">Read more<\/a>). However, sometimes you may require a programmatic method for that.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In case you are integrating third-party services or product information management systems with your Magento 2 store, you may require a programmatic method to set the URL key of the category pages.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In this blog post, I will show you how to set Magento 2 category URL key programmatically.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Let&#8217;s begin.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Method to Programmatically Set URL Key of Magento 2 Category Page<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">You can set the URL key of category programmatically in Magento 2 using the following code:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/\/categoryFactory  is object of class MagentoCatalogModelCategoryFactory\n \n$categoryFactory = $this->categoryFactory->create();\n$categoryFactory->setName(\"test);\n$categoryFactory->setCustomAttributes([\n    'url_key' => \"test-url\",\n]);\n \ntry \n{\n    $categoryFactory->save();\n} catch (Exception $e) {\n    echo $e->getMessage();\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">That&#8217;s it!<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">I hope this solution will help you with setting Magento 2 category URL key programmatically.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In case you are facing any issues or have any doubts regarding the solution, feel free to ask. I would be happy to help you.  <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Also, do not forget to share this Magento 2 solution with your developer friends via social media.  <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Thanks for reading.  <\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this blog post, I will show you the complete method to programmatically set URL key of category in Magento 2. Categorizing the products into&#8230;<\/p>\n","protected":false},"author":13,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[34],"tags":[],"class_list":["post-2105","post","type-post","status-publish","format-standard","hentry","category-magento"],"acf":[],"_links":{"self":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/2105","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/users\/13"}],"replies":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/comments?post=2105"}],"version-history":[{"count":1,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/2105\/revisions"}],"predecessor-version":[{"id":5021,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/2105\/revisions\/5021"}],"wp:attachment":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/media?parent=2105"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/categories?post=2105"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/tags?post=2105"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}