Simply add more conditions to the if( 'CPT1' != $post->post_type )
construct. Separate and describe the relationship between condition expressions using logical operators:
function wpse_222797a_flatten_hierarchies( $post_link, $post ) {
// IF( 'CPT1' IS NOT the post_type
// AND 'CPT2' IS NOT the post_type
// AND 'CPT3' IS NOT the post_type )
// THEN exit the function and RETURN the unmodified link.
if( 'CPT1' != $post->post_type
&& 'CPT2' != $post->post_type
&& 'CPT3' != $post->post_type )
return $post_link;
// If execution reaches this point, the post_type must be 'CPT1', 'CPT2',
// or 'CPT3', so produce and return a modified link.
// [...]
}
add_filter( 'post_type_link', 'wpse_222797a_flatten_hierarchies', 10, 2 );
Alternately, check the current post type against an array of post-types requiring link modification by leveraging in_array()
:
function wpse_222797b_flatten_hierarchies( $post_link, $post ) {
$modified_post_types = [ 'CPT1', 'CPT2', 'CPT3' ];
// IF the post_type IS NOT in the $modified_post_types array
// THEN exit the function and return the unmodified link
if ( ! in_array( $post->post_type, $modified_post_types ) )
return $post_link;
// If execution reaches this point, the post_type must be one of the ones
// in the $modified_post_types array, so produce and return a modified link.
// [...]
}
add_filter( 'post_type_link', 'wpse_222797b_flatten_hierarchies', 10, 2 );