Last active
October 19, 2016 18:46
-
-
Save nbeers22/3027c47067b126515aefc78a2e513e78 to your computer and use it in GitHub Desktop.
WordPress - Remove the custom post type name from the URL
This file contains hidden or 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
<?php | |
// Remove custom post type name from URL for special posts | |
function remove_cpt_slug( $post_link, $post, $leavename ) { | |
if ( 'special' != $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', 'remove_cpt_slug', 10, 3 ); | |
function parse_request_trick( $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', 'page', 'special' ) ); | |
} | |
} | |
add_action( 'pre_get_posts', 'parse_request_trick' ); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment