I use this code to "convert" custom post type in /customtype/postname.html
function rewrite_rules( $rules ) {
$new_rules = array();
foreach ( get_post_types() as $t )
$new_rules[ $t . '/([^/]+)\.html$' ] = 'index.php?post_type=' . $t . '&name=$matches[1]';
return $new_rules + $rules;
}
add_filter( 'post_type_link', 'custom_post_permalink' ); // for cpt post_type_link (rather than post_link)
function custom_post_permalink ( $post_link ) {
global $post;
$type = get_post_type( $post->ID );
return home_url( $type . '/' . $post->post_name . '.html' );
}
It works so well.
But !
If you create a DRAFT post and after put cursor on PREVIEW (in wp - admin)
You will see url like this : /.html?preview=true
How I can fix it ? That problem only with "PREVIEW" link.
I use this code to "convert" custom post type in /customtype/postname.html
function rewrite_rules( $rules ) {
$new_rules = array();
foreach ( get_post_types() as $t )
$new_rules[ $t . '/([^/]+)\.html$' ] = 'index.php?post_type=' . $t . '&name=$matches[1]';
return $new_rules + $rules;
}
add_filter( 'post_type_link', 'custom_post_permalink' ); // for cpt post_type_link (rather than post_link)
function custom_post_permalink ( $post_link ) {
global $post;
$type = get_post_type( $post->ID );
return home_url( $type . '/' . $post->post_name . '.html' );
}
It works so well.
But !
If you create a DRAFT post and after put cursor on PREVIEW (in wp - admin)
You will see url like this : http://www.domain.com/customtype/.html?preview=true
How I can fix it ? That problem only with "PREVIEW" link.
Here is a simple solution, if post have DRAFT status - we show a "normal" do not rewrited "URL".
add_filter( 'post_type_link', 'custom_post_permalink' ); // for cpt post_type_link (rather than post_link)
function custom_post_permalink ( $post_link ) {
global $post;
$type = get_post_type( $post->ID );
$post_status = get_post_status( $post->ID ); //get POST STATUS
if($post_status != "draft") {
return home_url('PUT YOUR CUSTOM POST TYPE HERE/'.$post->post_name.'.html');
} else {
//if POST STATUS is DRAFT
return home_url('?post_type=PUT YOUR CUSTOM POST TYPE HERE&p='.$post->ID.'&preview=true');
}
}