Change URL Prefix For Custom Post Types
While developing our own website with WordPress I came across one issue with the URL structure of blog and portfolio pages. I needed to have the blog post URL be axiswebart.com/blog/posturl. To achieve this I used WordPress permalinks settings and added a URL prefix using custom permalinks options as blog/%postname%/
This solved one problem with the blog URL and created another one. Now all our portfolio pages also started to have this URL prefix and all URL portfolio URLs started to have two URL prefix with a structure like axiswebart.com/blog/portfolio/portfolioitmeurl
How to Change URL Prefix For Custom Post Types in WordPress
We don’t want to have this kind of structure at all. After searching around the internet I came across the below solution to fix the problem –
We have registered a custom post type for our portfolio and I added the below line of code in the post type register function of the plugin.
'rewrite' => array(
'with_front' => false,
'slug' => 'portfolio'
)
Hope this helps anyone looking for a solution to a problem like this with a custom post type. Even if your URL doesn’t work after adding this code, make sure you refresh your rewrite rules stored in the wp_options table. To refresh that try saving your permalinks settings to default once and then reset it back to your original settings. It will regenerate all rewrite rules and your custom post type URL will start working as per your settings.