How to Change Custom Post Type Permalinks in WordPress

WordPress is the most popular Content Management System (CMS) in the world. There are over 64.2% websites on the Internet using it as their backend.

It’s not surprising that WordPress is so widely adopted. It’s so easy-to-use that someone with no web design knowledge can create a website in only a few minutes.

One of the most important parts of a WordPress’s website is permalinks (permanent links). They’re links that customers use to access your site or different pages on your website. SEO bots also use these links to access and rate your webpage’s ranking.

That’s why customizing and optimizing these permalinks are important. This guide will show you how to change custom post type permalinks.

What Are Custom Post Types?

You can post many types of content using WordPress. You can create posts, pages, attachments, navigation menus, and more by default.

Here’s one of WordPress’s greatest magic. If you want to publish posts other than these default types, you can. These posts are called custom post types (CPTs). They allow you to post unique content with different formats and structures than the WordPress standard post types.

Let’s say you’re building a product review website. Other than pure-text review articles, you may also want to post picture collages, customer testimonials, and scoreboards. WordPress doesn’t offer these post types by default. You must add these CPTs to the system using codes or plugins.

Here are some other examples of WordPress custom post types:

pda-wordpress-custom-post-type-examples

Understanding Custom Post Type Permalinks

WordPress has six default permalink formats. You can view them by logging into your admin panel, going to the Settings area, and clicking on Permalinks. You can select different types of permalinks and apply them to your website.

pda-wordpress-permalink-settings

While these permalinks are optimized for SEO, they may not look very good. Fortunately, you can make these links look far neater and more user-friendly.

Let’s re-use the example above and say that you’re building a product review website. Your link can show up as:

http://example.com/review/vacuum-cleaner

Instead of something boring like:

http://example.com/wordpress/2018/vacuum-cleaner

Interested? In the next section, we’ll show you how to change the custom post type permalink on your WordPress page.

How to Change the Custom Post Type Permalink in WordPress

There are two main ways to change the custom post type permalink in WordPress. You can use a plugin (this is the easiest method) or a custom piece of code.

#1 Install a Plugin

The best plugin to do this is the Custom Post Type Permalinks plugin. Once you have installed it, navigate to Settings > Permalinks in the WordPress admin dashboard. Scroll down and find the Permalink Setting for Custom Post Types section.

pda-permalink-settings-for-custom-post-types

Through this area, you can change the custom post type permalink structure on your WordPress site. Using it is extremely simple. One concept that you need to know is the Post ID. It’s the categories in which articles on your page sit, for example, reviews, photos, or clips.

Want your permalink to reflect the post type? Navigate to the empty box in the plugin and type in this tag:

/%post_id%/

The slashes at the tag’s beginning and end are extremely important. Without them, the tag won’t work.

Once you’ve added it in, the permalinks for your custom post types should now look something like this:

http://example.com/review/review_article

Press Save Changes to save the new permalink structure to your WordPress page, and voilà! You now have a custom post type permalink structure.

Other than the Post ID, you can experiment with other tags to achieve the same effect, too, like:

/%year%/

… which will display the publication year of the post. Or:

/%post_name%/

… which will display the name of the post.

The possibilities are endless!

Bonus Tip

The Protect WordPress Pages & Posts plugin comes as another great solution in case of changing custom post type permalinks. However, it works a bit different from the above plugin. Let us explain.

pda-protect-pages-and-posts-addon

The plugin provides the simplest solution to protect your private posts, pages, and custom post types. Once protected, they cannot be accessed directly through their original URLs anymore. Unauthorized users will be redirected to an error page when attempting to view these posts.

The only way to view the protected content is via the private access links that the plugin generates. In other words, only those who have the private links can access your private content. You can stop people from sharing the links with others by setting access limits and expiry time.

What’s more, the plugin enables you to customize the private links. You can change your post, page, and custom post type permalinks however you wish.

#2 Use Custom Coding

Using a custom piece of code to add new custom post type permalinks is more versatile. But it’s also a lot more complicated and risky.

If you don’t know what you’re doing, you’ll be at risk of corrupting the coding base of your site. So, unless you have experience working with back-end coding, we highly suggest you use the plugin instead.

There are four distinct steps. The first step involves registering the custom post type. You can use this piece of code for that job:

add_action('init', function() {
register_post_type('reference', [
'label' => __('References', 'txtdomain'),
'public' => true,
'menu_position' => 5,
'menu_icon' => 'dashicons-book',
'supports' => ['title', 'editor', 'thumbnail', 'author', 'custom-fields', 'revisions'],
'show_in_rest' => true,
'rewrite' => [
'slug' => (!empty(get_option('mytheme_reference_slug'))) ? get_option('mytheme_reference_slug') : 'reference',
'with_front' => false
],
'labels' => [
'singular_name' => __('Reference', 'txtdomain'),
'add_new_item' => __('Add new reference', 'txtdomain'),
'new_item' => __('New reference', 'txtdomain'),
'view_item' => __('View reference', 'txtdomain'),
'not_found' => __('No references found', 'txtdomain'),
'not_found_in_trash' => __('No references found in trash', 'txtdomain'),
'all_items' => __('All references', 'txtdomain'),
'insert_into_item' => __('Insert into reference', 'txtdomain')
], 
]);
});

The next part is to register the settings for the code by hooking the function to the admin_init hook. This step basically tells WordPress what function it would run to output the setting.

add_action('admin_init', function() {
add_settings_field('mytheme_reference_slug', __('References base', 'txtdomain'), 'mytest_reference_slug_output', 'permalink', 'optional');
});

As you can see, one of the arguments for the setting is ‘permalink’. This tells WordPress to add the new setting to the Permalinks page in the Settings panel in WordPress.

Next, we define the function that WordPress needs to run when it outputs the setting.

function mytest_reference_slug_output() {
?>
<input name="mytheme_reference_slug" type="text" class="regular-text code" value="<?php echo esc_attr(get_option('mytheme_reference_slug')); ?>" placeholder="<?php echo 'reference'; ?>" />
<?php
}

Last but not least, capping up the code is a function to save the setting.

add_action('admin_init', function() {
if (isset($_POST['permalink_structure'])) {
update_option('mytheme_reference_slug', trim($_POST['mytheme_reference_slug']));
}
});

So, altogether, you have this piece of code. Look into the Settings panel, then the Permalinks page. There, you’ll find an option to define the slug base. The custom post type will use whatever slug you define.

Let’s Create Your Own Custom Post Type Permalinks!

Most users won’t need to bother changing the custom post type permalinks. While the default structure may not look very aesthetically pleasing, it is SEO-optimized. Plus, most customers won’t look at the URL to judge the quality of your website’s content.

Still, if you want to give your website a special look, we hope this guide on changing the custom post type permalinks for WordPress has proven useful.

If you have any other questions, let us know in the comment section below. We’ll get back to you as soon as possible!

Leave a Reply

Your email address will not be published. Required fields are marked *