Skip to content

Instantly share code, notes, and snippets.

@stefanRepac
Created December 2, 2019 22:10
Show Gist options
  • Save stefanRepac/6ffb90f1ff2240a4da90af6eb776b430 to your computer and use it in GitHub Desktop.
Save stefanRepac/6ffb90f1ff2240a4da90af6eb776b430 to your computer and use it in GitHub Desktop.
Remove the slug from published post permalinks. Only affect our CPT though.
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 );
@yfchild
Copy link

yfchild commented Dec 2, 2019

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' );

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment