I created custom query var. So I need to include 404 page if query var not exists.

function add_rewrites(){
    add_rewrite_rule(
        '^custom_post_type/([^/]*)/([^/]*)/?',
        'index.php?post_type=custom_post_type&name=$matches[1]&sub_pages=$matches[2]',
        'top'
    );
}
add_action( 'init', 'add_rewrites' );

function add_custom_query_var( $vars ){ 
$vars[] = "sub_pages"; 
return $vars; 
} 
add_filter( 'query_vars', 'add_custom_query_var' );

For example:
if( get_query_var( ‘sub_pages’ ) == ” ){
//Normal Content
}elseif( get_query_var( ‘sub_pages’ ) == ‘example’ ){
//Example Content
}else{
//Include or Load 404
}

Read more here: including 404 page in post template


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: