wordpress custom url

In this WordPress tutorial we will learn how to include custom category or taxonomy term in url of custom post type.

Let's say we have a custom post type course. And we also have a custom taxonomy subject for this post type.

Step 1: Create A Custom Content Type

Let's create a custom content type course. Add below codes in functions.php file of your theme.


/**
 * Create custom content type couse
 */
function dewcodes_course_post_type() {
register_post_type( 'course',
// options for course content type.
  array(
    'labels' => array(
     'name' => __( 'Course' ),
     'singular_name' => __( 'course' )
    ),
    'public' => true,
    'has_archive' => true,
    'rewrite' => array('slug' => 'course'),
    'show_in_rest' => true,
    'supports' => array(
      'title',
      'editor',
      'excerpt',
      'comments',
      'thumbnail',
    )
  )
);
}
// Hooking up our function to theme setup
add_action( 'init', 'dewcodes_course_post_type' );

After adding above codes, you should have a custom content type Course in your WordPress website.

Step 2: Create A Custom Taxonomy

Now we will add a custom taxonomy Subjects for our content type Course.
Add following codes in functions.php file of your theme.


/**
 * Create Subjects taxanomy for content type Couse
 */
function dewcodes_subject_taxonomy() {
  register_taxonomy(
    'subject_taxonomy',
    'course', // This is content type.
    array(
      'label' => __( 'Subjects' ),
      'rewrite' => array( 'slug' => 'subject', 'with_front' => false ),
      'hierarchical' => true,
    )
  );
}
add_action( 'init', 'dewcodes_subject_taxonomy' );

After adding above codes, you should have a custom taxonomy Subjects for content type course.

Default Permalinks Structure

Now if we create a new course type post, its url will be like:

example.com/course/post-title

The above url do not have subject name in the course post url.

Add Subject name in course url

We want to include subject name in the url of course content type. Like below:

example.com/course/subject-name/post-title

Edit dewcodes_course_post_type function

In the function dewcodes_course_post_type that we have created above in step 1, change the value of rewrite. So, the new rewrite should be:


'rewrite' => array('slug' => 'course/%subject_taxonomy%'),

Step #3: Add Filter

Now add filter to post_type_link to include subject name in course url.
Add following codes in functions.php file of your theme.


function dewcodes_course_post_permalinks( $post_link, $post ){
  if ( is_object( $post ) && $post->post_type == 'course' ){
    $terms = wp_get_object_terms( $post->ID, 'subject_taxonomy' );
    if( $terms ){
      return str_replace( '%subject_taxonomy%' , $terms[0]->slug , $post_link );
    }
  }
  return $post_link;
}
add_filter( 'post_type_link', 'dewcodes_course_post_permalinks', 1, 2 );

Step #4: Update Permalinks

Navigate to:
WordPress Dashboard arrow Settings arrow Permalinks

And save the settings.

You should now have course urls with subject name like:

example.com/course/subject-name/post-title

Hope this article helped you. If you have any doubt, please comment below.

Tags