I’ve seen several posts lately with people easily removing the slugs from a single custom post type via the following function:

<?php  
function remove_cpt_slug( $post_link, $post, $leavename ) { 
    if ( 'custom_post_type1' != $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', 'custom_post_type1' ) );
    }
}
add_action( 'pre_get_posts', 'parse_request_trick' );

However I’ve been unable to get this to work when I add in multiple custom post types such as
'custom_post_type1'!= $post->post_type || 'custom_post_type2' != $post->post_type

$query->set( 'post_type', array( 'post', 'page', 'custom_post_type1', 'custom_post_type2 ) );

Can anyone help with this/explain?

Read more here: How to remove slugs from multiple custom post types


Solution:

If you know the solution of this issue, please leave us a reply in Comment section, to update the question.


Wordpress related questions and answers: