Created
December 2, 2019 22:10
-
-
Save stefanRepac/6ffb90f1ff2240a4da90af6eb776b430 to your computer and use it in GitHub Desktop.
Remove the slug from published post permalinks. Only affect our CPT though.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function wpex_remove_cpt_slug( $post_link, $post, $leavename ) { | |
if ( 'yourCustomPostSlug' != $post->post_type || 'publish' != $post->post_status ) { | |
return $post_link; | |
} | |
$post_link = str_replace( '/' . $post->post_type . '/', '/', $post_link ); | |
return $post_link; | |
} | |
add_filter( 'post_type_link', 'wpex_remove_cpt_slug', 10, 3 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
the same : With another languages, redirection doesn' t work, we go to homepage defaut.
function wpex_remove_cpt_slug( $post_link, $post, $leavename ) {
if ( 'tour' != $post->post_type || 'publish' != $post->post_status ) {
return $post_link;
}
$post_link = str_replace( '/' . $post->post_type . '/', '/', $post_link );
return $post_link;
}
add_filter( 'post_type_link', 'wpex_remove_cpt_slug', 10, 3 );
/I put second part from original post - nothing, a lot of people problem with xxx languages, is qworking with 1 language,*/
function wpex_parse_request_tricksy( $query ) {
// Only noop the main query
if ( ! $query->is_main_query() )
return;
// Only noop our very specific rewrite rule match
if ( 2 != count( $query->query ) || ! isset( $query->query['page'] ) ) {
return;
}
// 'name' will be set if post permalinks are just post_name, otherwise the page rule will match
if ( ! empty( $query->query['name'] ) ) {
$query->set( 'post_type', array( 'post', 'portfolio', 'page', 'tour' ) );
}
}
add_action( 'pre_get_posts', 'wpex_parse_request_tricksy' );