The Stack Overflow podcast is back! Listen to an interview with our new CEO.
4 deleted 5957 characters in body
source | link

Update:

I did test the above code snippet on the following setup:

1) Let's assume the new post type is car.

add_action( 'init', 'custom_rewrites_init' );
function custom_rewrites_init(){

  $labels = array(
    'name' => 'Cars',
    'singular_name' => 'Car',
    'add_new' => 'Add New',
    'add_new_item' => 'Add New Car',
    'edit_item' => 'Edit Car',
    'new_item' => 'New Car',
    'all_items' => 'All Cars',
    'view_item' => 'View Car',
    'search_items' => 'Search Cars',
    'not_found' =>  'No Cars found',
    'not_found_in_trash' => 'No Cars found in Trash', 
    'parent_item_colon' => '',
    'menu_name' => 'Cars'
  );

  $args = array(
     'rewrite' => array(
        'slug' => 'car/%post_id%',
        'with_front' => false,
        'pages' => true,
        'ep_mask' => 1,
    ),
    'labels' => $labels,
    'public' => true,
    'publicly_queryable' => true,
    'show_ui' => true, 
    'show_in_menu' => true, 
    'query_var' => true,
    'capability_type' => 'post',
    'has_archive' => true, 
    'hierarchical' => false,
    'menu_position' => null,
    'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments' )
  ); 

  register_post_type( 'car', $args );

}

2) The above will give use the following rewrite rules:

    [car/[0-9]+/[^/]+/attachment/([^/]+)/?$] => index.php?attachment=$matches[1]
    [car/[0-9]+/[^/]+/attachment/([^/]+)/trackback/?$] => index.php?attachment=$matches[1]&tb=1
    [car/[0-9]+/[^/]+/attachment/([^/]+)/feed/(feed|rdf|rss|rss2|atom)/?$] => index.php?attachment=$matches[1]&feed=$matches[2]
    [car/[0-9]+/[^/]+/attachment/([^/]+)/(feed|rdf|rss|rss2|atom)/?$] => index.php?attachment=$matches[1]&feed=$matches[2]
    [car/[0-9]+/[^/]+/attachment/([^/]+)/comment-page-([0-9]{1,})/?$] => index.php?attachment=$matches[1]&cpage=$matches[2]
    [car/([0-9]+)/([^/]+)/trackback/?$] => index.php?p=$matches[1]&car=$matches[2]&tb=1
    [car/([0-9]+)/([^/]+)/feed/(feed|rdf|rss|rss2|atom)/?$] => index.php?p=$matches[1]&car=$matches[2]&feed=$matches[3]
    [car/([0-9]+)/([^/]+)/(feed|rdf|rss|rss2|atom)/?$] => index.php?p=$matches[1]&car=$matches[2]&feed=$matches[3]
    [car/([0-9]+)/([^/]+)/page/?([0-9]{1,})/?$] => index.php?p=$matches[1]&car=$matches[2]&paged=$matches[3]
    [car/([0-9]+)/([^/]+)/comment-page-([0-9]{1,})/?$] => index.php?p=$matches[1]&car=$matches[2]&cpage=$matches[3]
    [car/([0-9]+)/([^/]+)(/[0-9]+)?/?$] => index.php?p=$matches[1]&car=$matches[2]&page=$matches[3]
    [car/[0-9]+/[^/]+/([^/]+)/?$] => index.php?attachment=$matches[1]
    [car/[0-9]+/[^/]+/([^/]+)/trackback/?$] => index.php?attachment=$matches[1]&tb=1
    [car/[0-9]+/[^/]+/([^/]+)/feed/(feed|rdf|rss|rss2|atom)/?$] => index.php?attachment=$matches[1]&feed=$matches[2]
    [car/[0-9]+/[^/]+/([^/]+)/(feed|rdf|rss|rss2|atom)/?$] => index.php?attachment=$matches[1]&feed=$matches[2]
    [car/[0-9]+/[^/]+/([^/]+)/comment-page-([0-9]{1,})/?$] => index.php?attachment=$matches[1]&cpage=$matches[2]
    [car/[0-9]+/attachment/([^/]+)/?$] => index.php?attachment=$matches[1]
    [car/[0-9]+/attachment/([^/]+)/trackback/?$] => index.php?attachment=$matches[1]&tb=1
    [car/[0-9]+/attachment/([^/]+)/feed/(feed|rdf|rss|rss2|atom)/?$] => index.php?attachment=$matches[1]&feed=$matches[2]
    [car/[0-9]+/attachment/([^/]+)/(feed|rdf|rss|rss2|atom)/?$] => index.php?attachment=$matches[1]&feed=$matches[2]
    [car/[0-9]+/attachment/([^/]+)/comment-page-([0-9]{1,})/?$] => index.php?attachment=$matches[1]&cpage=$matches[2]
    [car/([0-9]+)/trackback/?$] => index.php?p=$matches[1]&tb=1
    [car/([0-9]+)/feed/(feed|rdf|rss|rss2|atom)/?$] => index.php?p=$matches[1]&feed=$matches[2]
    [car/([0-9]+)/(feed|rdf|rss|rss2|atom)/?$] => index.php?p=$matches[1]&feed=$matches[2]
    [car/([0-9]+)/page/?([0-9]{1,})/?$] => index.php?p=$matches[1]&paged=$matches[2]
    [car/([0-9]+)/comment-page-([0-9]{1,})/?$] => index.php?p=$matches[1]&cpage=$matches[2]
    [car/([0-9]+)(/[0-9]+)?/?$] => index.php?p=$matches[1]&page=$matches[2]
    [car/[0-9]+/([^/]+)/?$] => index.php?attachment=$matches[1]
    [car/[0-9]+/([^/]+)/trackback/?$] => index.php?attachment=$matches[1]&tb=1
    [car/[0-9]+/([^/]+)/feed/(feed|rdf|rss|rss2|atom)/?$] => index.php?attachment=$matches[1]&feed=$matches[2]
    [car/[0-9]+/([^/]+)/(feed|rdf|rss|rss2|atom)/?$] => index.php?attachment=$matches[1]&feed=$matches[2]
    [car/[0-9]+/([^/]+)/comment-page-([0-9]{1,})/?$] => index.php?attachment=$matches[1]&cpage=$matches[2]

3) We need to add a support for for domain.com/car/ and domain.com/car/page/2.

That can be achieved with:

add_filter( 'rewrite_rules_array', 'custom_rewrite_rules_array' );
function custom_rewrite_rules_array( $rules ) {
        $newrules = array();
        $newrules['^car/page/?([0-9]{1,})/?$'] = 'index.php?post_type=post-type&paged=$matches[1]';
        $newrules['^car$'] = 'index.php?post_type=post-type';
       return $newrules + $rules;
}

It will give us these rewrite rules on top of the other ones:

[^car/page/?([0-9]{1,})/?$] => index.php?post_type=car&paged=$matches[1]
[^car$] => index.php?post_type=car

after we have saved the permalinks.

4) To handle the %post_id%, we use your code snippet:

add_filter('post_type_link', 'custom_post_type_link', 1, 3);
function custom_post_type_link( $post_link, $post = 0, $leavename = false ) {
    if ($post->post_type == 'car' ) {
        return str_replace('%post_id%', $post->ID, $post_link);
    } else {
        return $post_link;
    }
}

Update:

I did test the above code snippet on the following setup:

1) Let's assume the new post type is car.

add_action( 'init', 'custom_rewrites_init' );
function custom_rewrites_init(){

  $labels = array(
    'name' => 'Cars',
    'singular_name' => 'Car',
    'add_new' => 'Add New',
    'add_new_item' => 'Add New Car',
    'edit_item' => 'Edit Car',
    'new_item' => 'New Car',
    'all_items' => 'All Cars',
    'view_item' => 'View Car',
    'search_items' => 'Search Cars',
    'not_found' =>  'No Cars found',
    'not_found_in_trash' => 'No Cars found in Trash', 
    'parent_item_colon' => '',
    'menu_name' => 'Cars'
  );

  $args = array(
     'rewrite' => array(
        'slug' => 'car/%post_id%',
        'with_front' => false,
        'pages' => true,
        'ep_mask' => 1,
    ),
    'labels' => $labels,
    'public' => true,
    'publicly_queryable' => true,
    'show_ui' => true, 
    'show_in_menu' => true, 
    'query_var' => true,
    'capability_type' => 'post',
    'has_archive' => true, 
    'hierarchical' => false,
    'menu_position' => null,
    'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments' )
  ); 

  register_post_type( 'car', $args );

}

2) The above will give use the following rewrite rules:

    [car/[0-9]+/[^/]+/attachment/([^/]+)/?$] => index.php?attachment=$matches[1]
    [car/[0-9]+/[^/]+/attachment/([^/]+)/trackback/?$] => index.php?attachment=$matches[1]&tb=1
    [car/[0-9]+/[^/]+/attachment/([^/]+)/feed/(feed|rdf|rss|rss2|atom)/?$] => index.php?attachment=$matches[1]&feed=$matches[2]
    [car/[0-9]+/[^/]+/attachment/([^/]+)/(feed|rdf|rss|rss2|atom)/?$] => index.php?attachment=$matches[1]&feed=$matches[2]
    [car/[0-9]+/[^/]+/attachment/([^/]+)/comment-page-([0-9]{1,})/?$] => index.php?attachment=$matches[1]&cpage=$matches[2]
    [car/([0-9]+)/([^/]+)/trackback/?$] => index.php?p=$matches[1]&car=$matches[2]&tb=1
    [car/([0-9]+)/([^/]+)/feed/(feed|rdf|rss|rss2|atom)/?$] => index.php?p=$matches[1]&car=$matches[2]&feed=$matches[3]
    [car/([0-9]+)/([^/]+)/(feed|rdf|rss|rss2|atom)/?$] => index.php?p=$matches[1]&car=$matches[2]&feed=$matches[3]
    [car/([0-9]+)/([^/]+)/page/?([0-9]{1,})/?$] => index.php?p=$matches[1]&car=$matches[2]&paged=$matches[3]
    [car/([0-9]+)/([^/]+)/comment-page-([0-9]{1,})/?$] => index.php?p=$matches[1]&car=$matches[2]&cpage=$matches[3]
    [car/([0-9]+)/([^/]+)(/[0-9]+)?/?$] => index.php?p=$matches[1]&car=$matches[2]&page=$matches[3]
    [car/[0-9]+/[^/]+/([^/]+)/?$] => index.php?attachment=$matches[1]
    [car/[0-9]+/[^/]+/([^/]+)/trackback/?$] => index.php?attachment=$matches[1]&tb=1
    [car/[0-9]+/[^/]+/([^/]+)/feed/(feed|rdf|rss|rss2|atom)/?$] => index.php?attachment=$matches[1]&feed=$matches[2]
    [car/[0-9]+/[^/]+/([^/]+)/(feed|rdf|rss|rss2|atom)/?$] => index.php?attachment=$matches[1]&feed=$matches[2]
    [car/[0-9]+/[^/]+/([^/]+)/comment-page-([0-9]{1,})/?$] => index.php?attachment=$matches[1]&cpage=$matches[2]
    [car/[0-9]+/attachment/([^/]+)/?$] => index.php?attachment=$matches[1]
    [car/[0-9]+/attachment/([^/]+)/trackback/?$] => index.php?attachment=$matches[1]&tb=1
    [car/[0-9]+/attachment/([^/]+)/feed/(feed|rdf|rss|rss2|atom)/?$] => index.php?attachment=$matches[1]&feed=$matches[2]
    [car/[0-9]+/attachment/([^/]+)/(feed|rdf|rss|rss2|atom)/?$] => index.php?attachment=$matches[1]&feed=$matches[2]
    [car/[0-9]+/attachment/([^/]+)/comment-page-([0-9]{1,})/?$] => index.php?attachment=$matches[1]&cpage=$matches[2]
    [car/([0-9]+)/trackback/?$] => index.php?p=$matches[1]&tb=1
    [car/([0-9]+)/feed/(feed|rdf|rss|rss2|atom)/?$] => index.php?p=$matches[1]&feed=$matches[2]
    [car/([0-9]+)/(feed|rdf|rss|rss2|atom)/?$] => index.php?p=$matches[1]&feed=$matches[2]
    [car/([0-9]+)/page/?([0-9]{1,})/?$] => index.php?p=$matches[1]&paged=$matches[2]
    [car/([0-9]+)/comment-page-([0-9]{1,})/?$] => index.php?p=$matches[1]&cpage=$matches[2]
    [car/([0-9]+)(/[0-9]+)?/?$] => index.php?p=$matches[1]&page=$matches[2]
    [car/[0-9]+/([^/]+)/?$] => index.php?attachment=$matches[1]
    [car/[0-9]+/([^/]+)/trackback/?$] => index.php?attachment=$matches[1]&tb=1
    [car/[0-9]+/([^/]+)/feed/(feed|rdf|rss|rss2|atom)/?$] => index.php?attachment=$matches[1]&feed=$matches[2]
    [car/[0-9]+/([^/]+)/(feed|rdf|rss|rss2|atom)/?$] => index.php?attachment=$matches[1]&feed=$matches[2]
    [car/[0-9]+/([^/]+)/comment-page-([0-9]{1,})/?$] => index.php?attachment=$matches[1]&cpage=$matches[2]

3) We need to add a support for for domain.com/car/ and domain.com/car/page/2.

That can be achieved with:

add_filter( 'rewrite_rules_array', 'custom_rewrite_rules_array' );
function custom_rewrite_rules_array( $rules ) {
        $newrules = array();
        $newrules['^car/page/?([0-9]{1,})/?$'] = 'index.php?post_type=post-type&paged=$matches[1]';
        $newrules['^car$'] = 'index.php?post_type=post-type';
       return $newrules + $rules;
}

It will give us these rewrite rules on top of the other ones:

[^car/page/?([0-9]{1,})/?$] => index.php?post_type=car&paged=$matches[1]
[^car$] => index.php?post_type=car

after we have saved the permalinks.

4) To handle the %post_id%, we use your code snippet:

add_filter('post_type_link', 'custom_post_type_link', 1, 3);
function custom_post_type_link( $post_link, $post = 0, $leavename = false ) {
    if ($post->post_type == 'car' ) {
        return str_replace('%post_id%', $post->ID, $post_link);
    } else {
        return $post_link;
    }
}
3 added 5959 characters in body
source | link

Update:

I did test the above code snippet on the following setup:

1) Let's assume the new post type is car.

add_action( 'init', 'custom_rewrites_init' );
function custom_rewrites_init(){

  $labels = array(
    'name' => 'Cars',
    'singular_name' => 'Car',
    'add_new' => 'Add New',
    'add_new_item' => 'Add New Car',
    'edit_item' => 'Edit Car',
    'new_item' => 'New Car',
    'all_items' => 'All Cars',
    'view_item' => 'View Car',
    'search_items' => 'Search Cars',
    'not_found' =>  'No Cars found',
    'not_found_in_trash' => 'No Cars found in Trash', 
    'parent_item_colon' => '',
    'menu_name' => 'Cars'
  );

  $args = array(
     'rewrite' => array(
        'slug' => 'car/%post_id%',
        'with_front' => false,
        'pages' => true,
        'ep_mask' => 1,
    ),
    'labels' => $labels,
    'public' => true,
    'publicly_queryable' => true,
    'show_ui' => true, 
    'show_in_menu' => true, 
    'query_var' => true,
    'capability_type' => 'post',
    'has_archive' => true, 
    'hierarchical' => false,
    'menu_position' => null,
    'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments' )
  ); 

  register_post_type( 'car', $args );

}

2) The above will give use the following rewrite rules:

    [car/[0-9]+/[^/]+/attachment/([^/]+)/?$] => index.php?attachment=$matches[1]
    [car/[0-9]+/[^/]+/attachment/([^/]+)/trackback/?$] => index.php?attachment=$matches[1]&tb=1
    [car/[0-9]+/[^/]+/attachment/([^/]+)/feed/(feed|rdf|rss|rss2|atom)/?$] => index.php?attachment=$matches[1]&feed=$matches[2]
    [car/[0-9]+/[^/]+/attachment/([^/]+)/(feed|rdf|rss|rss2|atom)/?$] => index.php?attachment=$matches[1]&feed=$matches[2]
    [car/[0-9]+/[^/]+/attachment/([^/]+)/comment-page-([0-9]{1,})/?$] => index.php?attachment=$matches[1]&cpage=$matches[2]
    [car/([0-9]+)/([^/]+)/trackback/?$] => index.php?p=$matches[1]&car=$matches[2]&tb=1
    [car/([0-9]+)/([^/]+)/feed/(feed|rdf|rss|rss2|atom)/?$] => index.php?p=$matches[1]&car=$matches[2]&feed=$matches[3]
    [car/([0-9]+)/([^/]+)/(feed|rdf|rss|rss2|atom)/?$] => index.php?p=$matches[1]&car=$matches[2]&feed=$matches[3]
    [car/([0-9]+)/([^/]+)/page/?([0-9]{1,})/?$] => index.php?p=$matches[1]&car=$matches[2]&paged=$matches[3]
    [car/([0-9]+)/([^/]+)/comment-page-([0-9]{1,})/?$] => index.php?p=$matches[1]&car=$matches[2]&cpage=$matches[3]
    [car/([0-9]+)/([^/]+)(/[0-9]+)?/?$] => index.php?p=$matches[1]&car=$matches[2]&page=$matches[3]
    [car/[0-9]+/[^/]+/([^/]+)/?$] => index.php?attachment=$matches[1]
    [car/[0-9]+/[^/]+/([^/]+)/trackback/?$] => index.php?attachment=$matches[1]&tb=1
    [car/[0-9]+/[^/]+/([^/]+)/feed/(feed|rdf|rss|rss2|atom)/?$] => index.php?attachment=$matches[1]&feed=$matches[2]
    [car/[0-9]+/[^/]+/([^/]+)/(feed|rdf|rss|rss2|atom)/?$] => index.php?attachment=$matches[1]&feed=$matches[2]
    [car/[0-9]+/[^/]+/([^/]+)/comment-page-([0-9]{1,})/?$] => index.php?attachment=$matches[1]&cpage=$matches[2]
    [car/[0-9]+/attachment/([^/]+)/?$] => index.php?attachment=$matches[1]
    [car/[0-9]+/attachment/([^/]+)/trackback/?$] => index.php?attachment=$matches[1]&tb=1
    [car/[0-9]+/attachment/([^/]+)/feed/(feed|rdf|rss|rss2|atom)/?$] => index.php?attachment=$matches[1]&feed=$matches[2]
    [car/[0-9]+/attachment/([^/]+)/(feed|rdf|rss|rss2|atom)/?$] => index.php?attachment=$matches[1]&feed=$matches[2]
    [car/[0-9]+/attachment/([^/]+)/comment-page-([0-9]{1,})/?$] => index.php?attachment=$matches[1]&cpage=$matches[2]
    [car/([0-9]+)/trackback/?$] => index.php?p=$matches[1]&tb=1
    [car/([0-9]+)/feed/(feed|rdf|rss|rss2|atom)/?$] => index.php?p=$matches[1]&feed=$matches[2]
    [car/([0-9]+)/(feed|rdf|rss|rss2|atom)/?$] => index.php?p=$matches[1]&feed=$matches[2]
    [car/([0-9]+)/page/?([0-9]{1,})/?$] => index.php?p=$matches[1]&paged=$matches[2]
    [car/([0-9]+)/comment-page-([0-9]{1,})/?$] => index.php?p=$matches[1]&cpage=$matches[2]
    [car/([0-9]+)(/[0-9]+)?/?$] => index.php?p=$matches[1]&page=$matches[2]
    [car/[0-9]+/([^/]+)/?$] => index.php?attachment=$matches[1]
    [car/[0-9]+/([^/]+)/trackback/?$] => index.php?attachment=$matches[1]&tb=1
    [car/[0-9]+/([^/]+)/feed/(feed|rdf|rss|rss2|atom)/?$] => index.php?attachment=$matches[1]&feed=$matches[2]
    [car/[0-9]+/([^/]+)/(feed|rdf|rss|rss2|atom)/?$] => index.php?attachment=$matches[1]&feed=$matches[2]
    [car/[0-9]+/([^/]+)/comment-page-([0-9]{1,})/?$] => index.php?attachment=$matches[1]&cpage=$matches[2]

3) We need to add a support for for domain.com/car/ and domain.com/car/page/2.

That can be achieved with:

add_filter( 'rewrite_rules_array', 'custom_rewrite_rules_array' );
function custom_rewrite_rules_array( $rules ) {
        $newrules = array();
        $newrules['^car/page/?([0-9]{1,})/?$'] = 'index.php?post_type=post-type&paged=$matches[1]';
        $newrules['^car$'] = 'index.php?post_type=post-type';
       return $newrules + $rules;
}

It will give us these rewrite rules on top of the other ones:

[^car/page/?([0-9]{1,})/?$] => index.php?post_type=car&paged=$matches[1]
[^car$] => index.php?post_type=car

after we have saved the permalinks.

4) To handle the %post_id%, we use your code snippet:

add_filter('post_type_link', 'custom_post_type_link', 1, 3);
function custom_post_type_link( $post_link, $post = 0, $leavename = false ) {
    if ($post->post_type == 'car' ) {
        return str_replace('%post_id%', $post->ID, $post_link);
    } else {
        return $post_link;
    }
}

Update:

I did test the above code snippet on the following setup:

1) Let's assume the new post type is car.

add_action( 'init', 'custom_rewrites_init' );
function custom_rewrites_init(){

  $labels = array(
    'name' => 'Cars',
    'singular_name' => 'Car',
    'add_new' => 'Add New',
    'add_new_item' => 'Add New Car',
    'edit_item' => 'Edit Car',
    'new_item' => 'New Car',
    'all_items' => 'All Cars',
    'view_item' => 'View Car',
    'search_items' => 'Search Cars',
    'not_found' =>  'No Cars found',
    'not_found_in_trash' => 'No Cars found in Trash', 
    'parent_item_colon' => '',
    'menu_name' => 'Cars'
  );

  $args = array(
     'rewrite' => array(
        'slug' => 'car/%post_id%',
        'with_front' => false,
        'pages' => true,
        'ep_mask' => 1,
    ),
    'labels' => $labels,
    'public' => true,
    'publicly_queryable' => true,
    'show_ui' => true, 
    'show_in_menu' => true, 
    'query_var' => true,
    'capability_type' => 'post',
    'has_archive' => true, 
    'hierarchical' => false,
    'menu_position' => null,
    'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments' )
  ); 

  register_post_type( 'car', $args );

}

2) The above will give use the following rewrite rules:

    [car/[0-9]+/[^/]+/attachment/([^/]+)/?$] => index.php?attachment=$matches[1]
    [car/[0-9]+/[^/]+/attachment/([^/]+)/trackback/?$] => index.php?attachment=$matches[1]&tb=1
    [car/[0-9]+/[^/]+/attachment/([^/]+)/feed/(feed|rdf|rss|rss2|atom)/?$] => index.php?attachment=$matches[1]&feed=$matches[2]
    [car/[0-9]+/[^/]+/attachment/([^/]+)/(feed|rdf|rss|rss2|atom)/?$] => index.php?attachment=$matches[1]&feed=$matches[2]
    [car/[0-9]+/[^/]+/attachment/([^/]+)/comment-page-([0-9]{1,})/?$] => index.php?attachment=$matches[1]&cpage=$matches[2]
    [car/([0-9]+)/([^/]+)/trackback/?$] => index.php?p=$matches[1]&car=$matches[2]&tb=1
    [car/([0-9]+)/([^/]+)/feed/(feed|rdf|rss|rss2|atom)/?$] => index.php?p=$matches[1]&car=$matches[2]&feed=$matches[3]
    [car/([0-9]+)/([^/]+)/(feed|rdf|rss|rss2|atom)/?$] => index.php?p=$matches[1]&car=$matches[2]&feed=$matches[3]
    [car/([0-9]+)/([^/]+)/page/?([0-9]{1,})/?$] => index.php?p=$matches[1]&car=$matches[2]&paged=$matches[3]
    [car/([0-9]+)/([^/]+)/comment-page-([0-9]{1,})/?$] => index.php?p=$matches[1]&car=$matches[2]&cpage=$matches[3]
    [car/([0-9]+)/([^/]+)(/[0-9]+)?/?$] => index.php?p=$matches[1]&car=$matches[2]&page=$matches[3]
    [car/[0-9]+/[^/]+/([^/]+)/?$] => index.php?attachment=$matches[1]
    [car/[0-9]+/[^/]+/([^/]+)/trackback/?$] => index.php?attachment=$matches[1]&tb=1
    [car/[0-9]+/[^/]+/([^/]+)/feed/(feed|rdf|rss|rss2|atom)/?$] => index.php?attachment=$matches[1]&feed=$matches[2]
    [car/[0-9]+/[^/]+/([^/]+)/(feed|rdf|rss|rss2|atom)/?$] => index.php?attachment=$matches[1]&feed=$matches[2]
    [car/[0-9]+/[^/]+/([^/]+)/comment-page-([0-9]{1,})/?$] => index.php?attachment=$matches[1]&cpage=$matches[2]
    [car/[0-9]+/attachment/([^/]+)/?$] => index.php?attachment=$matches[1]
    [car/[0-9]+/attachment/([^/]+)/trackback/?$] => index.php?attachment=$matches[1]&tb=1
    [car/[0-9]+/attachment/([^/]+)/feed/(feed|rdf|rss|rss2|atom)/?$] => index.php?attachment=$matches[1]&feed=$matches[2]
    [car/[0-9]+/attachment/([^/]+)/(feed|rdf|rss|rss2|atom)/?$] => index.php?attachment=$matches[1]&feed=$matches[2]
    [car/[0-9]+/attachment/([^/]+)/comment-page-([0-9]{1,})/?$] => index.php?attachment=$matches[1]&cpage=$matches[2]
    [car/([0-9]+)/trackback/?$] => index.php?p=$matches[1]&tb=1
    [car/([0-9]+)/feed/(feed|rdf|rss|rss2|atom)/?$] => index.php?p=$matches[1]&feed=$matches[2]
    [car/([0-9]+)/(feed|rdf|rss|rss2|atom)/?$] => index.php?p=$matches[1]&feed=$matches[2]
    [car/([0-9]+)/page/?([0-9]{1,})/?$] => index.php?p=$matches[1]&paged=$matches[2]
    [car/([0-9]+)/comment-page-([0-9]{1,})/?$] => index.php?p=$matches[1]&cpage=$matches[2]
    [car/([0-9]+)(/[0-9]+)?/?$] => index.php?p=$matches[1]&page=$matches[2]
    [car/[0-9]+/([^/]+)/?$] => index.php?attachment=$matches[1]
    [car/[0-9]+/([^/]+)/trackback/?$] => index.php?attachment=$matches[1]&tb=1
    [car/[0-9]+/([^/]+)/feed/(feed|rdf|rss|rss2|atom)/?$] => index.php?attachment=$matches[1]&feed=$matches[2]
    [car/[0-9]+/([^/]+)/(feed|rdf|rss|rss2|atom)/?$] => index.php?attachment=$matches[1]&feed=$matches[2]
    [car/[0-9]+/([^/]+)/comment-page-([0-9]{1,})/?$] => index.php?attachment=$matches[1]&cpage=$matches[2]

3) We need to add a support for for domain.com/car/ and domain.com/car/page/2.

That can be achieved with:

add_filter( 'rewrite_rules_array', 'custom_rewrite_rules_array' );
function custom_rewrite_rules_array( $rules ) {
        $newrules = array();
        $newrules['^car/page/?([0-9]{1,})/?$'] = 'index.php?post_type=post-type&paged=$matches[1]';
        $newrules['^car$'] = 'index.php?post_type=post-type';
       return $newrules + $rules;
}

It will give us these rewrite rules on top of the other ones:

[^car/page/?([0-9]{1,})/?$] => index.php?post_type=car&paged=$matches[1]
[^car$] => index.php?post_type=car

after we have saved the permalinks.

4) To handle the %post_id%, we use your code snippet:

add_filter('post_type_link', 'custom_post_type_link', 1, 3);
function custom_post_type_link( $post_link, $post = 0, $leavename = false ) {
    if ($post->post_type == 'car' ) {
        return str_replace('%post_id%', $post->ID, $post_link);
    } else {
        return $post_link;
    }
}
2 added 3 characters in body
source | link

You could try to add this code snippet:

add_filter( 'rewrite_rules_array', 'custom_rewrite_rules_array' );
function custom_rewrite_rules_array( $rules ) {
        $newrules = array();
        $newrules['^post-type/page/?([0-9]{1,})/?$'] = 'index.php?post_type=post-type&paged=$matches[1]';
        $newrules['^post-type$'] = 'index.php?post_type=post-type';
       return $newrules + $rules;
}

to add support for domain.com/post-type/ and domain.com/post-type/page/2.

You just have to remember to save the permalinks.

You could try to add this code snippet:

add_filter('rewrite_rules_array','custom_rewrite_rules_array');
function custom_rewrite_rules_array( $rules ) {
        $newrules = array();
        $newrules['^post-type/page/?([0-9]{1,})/?$'] = 'index.php?post_type=post-type&paged=$matches[1]';
        $newrules['^post-type$'] = 'index.php?post_type=post-type';
       return $newrules + $rules;
}

to add support for domain.com/post-type/ and domain.com/post-type/page/2.

You just have to remember to save the permalinks.

You could try to add this code snippet:

add_filter( 'rewrite_rules_array', 'custom_rewrite_rules_array' );
function custom_rewrite_rules_array( $rules ) {
        $newrules = array();
        $newrules['^post-type/page/?([0-9]{1,})/?$'] = 'index.php?post_type=post-type&paged=$matches[1]';
        $newrules['^post-type$'] = 'index.php?post_type=post-type';
       return $newrules + $rules;
}

to add support for domain.com/post-type/ and domain.com/post-type/page/2.

You just have to remember to save the permalinks.

1
source | link