I've seen several posts lately with people easily removing the slugs from a single custom post type via the following function:
function hf_remove_slug( $post_link, $post, $leavename ) {
if(('pop-tags' == $post->post_type || 'furniture-type' == $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', 'hf_remove_slug', 10, 3 );
function hf_parse_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', 'pop-tags', 'furniture-type', 'page' ) );
}
}
add_action( 'pre_get_posts', 'hf_parse_request' );
However I've been unable to get this to work when I add in multiple custom post types. Can anyone help with this/explain?
'custom_post_type2
. You might also try reformatting the remove_cpt_slug function as a positive match:if(('custom_post_type1' == $post->post_type || 'custom_post_type2' == $post->post_type) && 'publish' == $post->post_status)
do your str_replace and return.