Skip to content

Instantly share code, notes, and snippets.

@toddsby
Forked from wpexplorer/gist:8244555
Created February 24, 2014 23:29
Show Gist options
  • Save toddsby/9199488 to your computer and use it in GitHub Desktop.
Save toddsby/9199488 to your computer and use it in GitHub Desktop.
/**
* Remove the slug from published post permalinks. Only affect our CPT though.
*/
function wpex_remove_post_type_slug( $post_link, $post, $leavename ) {
if ( ! in_array( $post->post_type, array( 'YOUR_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_post_type_slug', 10, 3 );
/**
* Some hackery to have WordPress match postname to any of our public post types
* All of our public post types can have /post-name/ as the slug, so they better be unique across all posts
* Typically core only accounts for posts and pages where the slug is /post-name/
*/
function wpex_post_type_slug_parse_request_edit( $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', 'YOUR_POST_TYPE', 'page' ) );
}
add_action( 'pre_get_posts', 'wpex_post_type_slug_parse_request_edit' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment