• I am creating a custom CPT where I have customized CPT URL structure of CPT, I am using the rewrite rule for this but it is conflicting This is a Lessons CPT, I want to remove lessons from the URL and if any category or subcategory is selected in the post then the URL structure should be parent category/child-category/post name, I have also customized the category URL structure where I want to remove lesson-category in this example:- https://localhost/custom_theme/lesson-category/california/ the URL should be like this https://localhost/custom_theme/california/

    I have Custom Lessons CPT where I want to customize the URl structure Here is the scenario:-

    I want to remove the lesson in Custom CPT – https://localhost/custom_theme/lesson/paino_lesson/ the URL should be like this https://localhost/custom_theme/paino_lesson/

    if any category or subcategory is selected in the post then the URL structure should be parent category/child category/post name,

    I want to remove the lesson category in this example:- https://localhost/custom_theme/lesson-category/california/ the URL should be like this https://localhost/custom_theme/california/

Viewing 4 replies - 1 through 4 (of 4 total)
  • Hello,
    I appreciate your question and understand your concern. Well here is some steps
    To achieve the desired URL structure in WordPress for your custom post type (CPT) and categories, you can use a combination of custom rewrite rules and filters. Below is an example of how you can achieve this:

    1. Register Custom Post Type (CPT):

    Make sure you have registered your custom post type with the appropriate arguments, including ‘rewrite’. Here’s an example:

    function custom_theme_register_lessons_cpt() {
        $labels = array(
            // Add your labels here
        );
    
        $args = array(
            'labels'      => $labels,
            'public'      => true,
            'has_archive' => true,
            'rewrite'     => array( 'slug' => 'lesson', 'with_front' => false ),
            // Add other arguments as needed
        );
    
        register_post_type( 'lessons', $args );
    }
    add_action( 'init', 'custom_theme_register_lessons_cpt' );
    

    Make sure to adjust the arguments according to your requirements.

    1. Remove ‘lesson’ from Custom Post Type URL:You can use the post_type_link filter to modify the permalink for your custom post type. Here’s an example:
    function custom_theme_lessons_permalink( $post_link, $post ) {
        if ( is_object( $post ) && $post->post_type == 'lessons' ) {
            $post_link = str_replace( '/lesson/', '/', $post_link );
        }
        return $post_link;
    }
    add_filter( 'post_type_link', 'custom_theme_lessons_permalink', 10, 2 );
    
    function custom_theme_lessons_permalink( $post_link, $post ) {
        if ( is_object( $post ) && $post->post_type == 'lessons' ) {
            $post_link = str_replace( '/lesson/', '/', $post_link );
        }
        return $post_link;
    }
    add_filter( 'post_type_link', 'custom_theme_lessons_permalink', 10, 2 );
    

    3. Custom Category URL Structure:

    For customizing the category URL structure, you can use the category_link filter. Here’s an example:

    function custom_theme_category_permalink( $termlink, $term, $taxonomy ) {
        if ( 'lesson-category' == $taxonomy ) {
            $termlink = str_replace( '/lesson-category/', '/', $termlink );
        }
        return $termlink;
    }
    add_filter( 'category_link', 'custom_theme_category_permalink', 10, 3 );
    
    1. Replace ‘lesson-category’ with the actual slug of your category taxonomy.

    Remember to flush the rewrite rules after adding or modifying these functions. You can do this by going to the WordPress admin dashboard, navigating to Settings > Permalinks, and clicking the “Save Changes” button.

    Hope you find your answer thank you.

    Moderator bcworkz

    (@bcworkz)

    What narolainfotech describes will cause WP to generate the links you desire, but you will still have a rewrite rule conflict. Only native posts and pages can have a permastruct of only the post name. Any other permastruct must have some static distinguishing element like /lesson/. It doesn’t have to literally be “lesson”, but you need something.

    If a post name only permastruct is needed for a lesson CPT, you could use the native post type for your lessons and create a new post type for blog posts, “blog” for example. Then lessons permastruct can be post name only, but blog permastructs will now need /blog/ as the static element. Assuming you have a need for blog posts. If not, just use the native post type for your lessons.

    Thread Starter neelusinghal

    (@neelusinghal)

    Hello

    I am working on my local server, and the folder name for my WordPress is “custom_theme.” I am currently testing the following code. However, it is not functioning as expected. When I add a lesson, like “simple lesson,” the resulting URL is https://localhost/custom_theme/simple-lesson/, leading to a 404 error.

    I am trying to figure out why it is not working as intended. Could you please assist me in understanding the reason behind this issue? Additionally, if I choose a category, such as “California,” the expected URL should be https://localhost/custom_theme/california/simple-lesson/.

    // START: CPT ‘Lessons’

    function register_lessons_post_type() {
    $labels = array(
    ‘name’ => _x( ‘Lessons’, ‘post type general name’, ‘essentials’ ),
    ‘singular_name’ => _x( ‘Lesson’, ‘post type singular name’, ‘essentials’ ),
    ‘menu_name’ => _x( ‘Lessons’, ‘admin menu’, ‘essentials’ ),
    ‘name_admin_bar’ => _x( ‘Lesson’, ‘add new on admin bar’, ‘essentials’ ),
    ‘add_new’ => _x( ‘Add New’, ‘lesson’, ‘essentials’ ),
    ‘add_new_item’ => ( ‘Add New Lesson’, ‘essentials’ ), ‘new_item’ => ( ‘New Lesson’, ‘essentials’ ),
    ‘edit_item’ => ( ‘Edit Lesson’, ‘essentials’ ), ‘view_item’ => ( ‘View Lesson’, ‘essentials’ ),
    ‘all_items’ => ( ‘All Lessons’, ‘essentials’ ), ‘search_items’ => ( ‘Search Lessons’, ‘essentials’ ),
    ‘not_found’ => ( ‘No lessons found’, ‘essentials’ ), ‘not_found_in_trash’ => ( ‘No lessons found in Trash’, ‘essentials’ ),
    );

    $args = array(
        'labels'             => $labels,
        'public'             => true,
        'publicly_queryable' => true,
        'show_ui'            => true,
        'show_in_menu'       => true,
        'query_var'          => true,
        // 'rewrite'            => array( 'slug' => 'lessons' ),
        'rewrite'     => array( 'slug' => 'lesson', 'with_front' => false ),
        'capability_type'    => 'post',
        'has_archive'        => true,
        'hierarchical'       => false,
        'menu_position'      => null,
        'supports'           => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments' ),
    );
    
    register_post_type( 'lessons', $args );

    }

    add_action( ‘init’, ‘register_lessons_post_type’ );

    function custom_theme_lessons_permalink( $post_link, $post ) {
    if ( is_object( $post ) && $post->post_type == ‘lessons’ ) {
    $post_link = str_replace( ‘/lesson/’, ‘/’, $post_link );
    }
    return $post_link;
    }
    add_filter( ‘post_type_link’, ‘custom_theme_lessons_permalink’, 10, 2 );

    function register_lessons_taxonomy() {
    $labels = array(
    ‘name’ => x( ‘Lesson Categories’, ‘taxonomy general name’, ‘essentials’ ), ‘singular_name’ => _x( ‘Lesson Category’, ‘taxonomy singular name’, ‘essentials’ ), ‘search_items’ => ( ‘Search Lesson Categories’, ‘essentials’ ), ‘all_items’ => ( ‘All Lesson Categories’, ‘essentials’ ), ‘parent_item’ => ( ‘Parent Lesson Category’, ‘essentials’ ), ‘parent_item_colon’ => ( ‘Parent Lesson Category:’, ‘essentials’ ), ‘edit_item’ => ( ‘Edit Lesson Category’, ‘essentials’ ), ‘update_item’ => ( ‘Update Lesson Category’, ‘essentials’ ), ‘add_new_item’ => ( ‘Add New Lesson Category’, ‘essentials’ ), ‘new_item_name’ => ( ‘New Lesson Category Name’, ‘essentials’ ), ‘menu_name’ => _( ‘Lesson Categories’, ‘essentials’ ),
    );

    $args = array(
        'hierarchical'      => true,
        'labels'            => $labels,
        'show_ui'           => true,
        'show_admin_column' => true,
        'query_var'         => true,
        'rewrite'           => array( 'slug' => 'lesson-category' ),
    );
    
    register_taxonomy( 'lesson_category', array( 'lessons' ), $args );

    }

    add_action( ‘init’, ‘register_lessons_taxonomy’ );

    function custom_theme_category_permalink( $termlink, $term, $taxonomy ) {
    if ( ‘lesson-category’ == $taxonomy ) {
    $termlink = str_replace( ‘/lesson-category/’, ‘/’, $termlink );
    }
    return $termlink;
    }
    add_filter( ‘category_link’, ‘custom_theme_category_permalink’, 10, 3 );

    Moderator bcworkz

    (@bcworkz)

    When you register the post type, the rewrite term is “lesson”, thus your lesson permalinks will be something like https://localhost/custom-theme/lesson/simple-lesson/

    If you were to remove the /lesson/ element from the URL in a request for that simple lesson, you will get a 404 because WP will think you are requesting a post of that name, which does not exist.

    While you could create a rewrite rule that does not require the /lesson/ element, it’ll conflict with the default post/page rewrite rule, causing post and page requests to go 404 instead.

    You could register your CPT with some other rewrite slug like “class”, “learn”, or even “l”, but you need something static and unchanging. Similar for the /taxonomy-term/post-slug/ type of request. You need some static element distinguishing this type of request from all others. Incorporate it into another rewrite rule that can match that pattern with its static element.

    Anytime you alter rewrite rules or related parameters, always remember what neelusinghal had told us:

    Remember to flush the rewrite rules after adding or modifying these functions. You can do this by going to the WordPress admin dashboard, navigating to Settings > Permalinks

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Custom post type URl Cutomization’ is closed to new replies.