Remove the slug from published custom post permalinks


/**
* Remove the slug from published custom post permalinks. Only affect our custom post type, though.
*/
function remove_custom_slug( $post_link, $post, $leavename ) {

if ( ‘product’ != $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_custom_slug’, 10, 3 );

function update_post_request( $query ) {

if ( ! $query->is_main_query() || 2 != count( $query->query ) || ! isset( $query->query[‘page’] ) ) {
return;
}

if ( ! empty( $query->query[‘name’] ) ) {
$query->set( ‘post_type’, array( ‘post’, ‘product’, ‘page’ ) );
}
}
add_action( ‘pre_get_posts’, ‘update_post_request’ );

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s