WordPress offers incredible flexibility when it comes to creating custom post types (CPTs) and taxonomies to organize your content. One crucial aspect of optimizing your website for search engines and improving user experience is creating clean and SEO-friendly URLs for your CPT single posts. In this guide, we’ll walk you through the steps to rewrite CPT single post URLs in WordPress, ensuring they are both user-friendly and SEO-friendly.
Open Your functions.php File
The first step in rewriting CPT single post URLs is to access and modify your theme or child theme’s functions.php file. This is where you’ll make the necessary adjustments to your CPT and taxonomy settings.
Define Custom Rewrite Rules for Your CPT
If you haven’t already created a custom post type, you can use the register_post_type() function to do so. This function allows you to set various parameters for your CPT, including the rewrite rules for its single posts.
For example, if your CPT is named “books,” you can add the following rewrite rules to the register_post_type() function:
'rewrite' => array('slug' => 'books/%book_category%', 'with_front' => false),
Here’s what each parameter does:
slug sets the base URL for your CPT single posts to “books.”
%book_category% is a placeholder for the book category taxonomy, which we’ll create next.
with_front is set to false to remove the main site prefix from the URL.
Create a Custom Taxonomy for Your CPT
To create a hierarchical structure in your URL, you’ll need to register a new taxonomy for your CPT. If you’re working with the “books” CPT, you can create a taxonomy called “book_category” with the following code:
register_taxonomy(
'book_category',
'books',
array(
'hierarchical' => true,
'label' => 'Book Categories',
'rewrite' => array('slug' => 'books', 'with_front' => false),
)
);
Just like with the CPT rewrite rules, the slug parameter defines the base URL for your taxonomy, and with_front is set to false to eliminate the main site prefix from the URL.
Save Your Changes
Make sure to save the changes you’ve made to your functions.php file.
Step 5: Flush Permalinks
After making these changes, you may need to flush your permalinks to ensure the new URL structure takes effect. To do this, navigate to your WordPress dashboard, go to Settings > Permalinks, and click the “Save Changes” button.
How to Do It Programmatically:
If you already have a CPT set up using a plugin like CPT UI and want to achieve the same result programmatically without altering your existing code, you can use the following code snippet:
function rewrite_library_permalinks( $post_link, $post ) {
if ( is_object( $post ) && 'books' === $post->post_type ) {
$terms = wp_get_object_terms( $post->ID, 'resource_type' );
if ( $terms ) {
return str_replace( '%resource_type%', $terms[0]->slug, $post_link );
}
}
return $post_link;
}
add_filter( 'post_type_link', 'rewrite_library_permalinks', 10, 2 );
Optimizing your WordPress website for search engines and user experience involves various strategies, and creating SEO-friendly URLs for your CPT single posts is one important aspect. By following these steps and using the provided code snippet, you can easily rewrite CPT single post URLs, improving both your site’s SEO and its overall navigability for visitors.