I'm not sure how you define "first child" - alphabetically, by highest or lowest ID, etc., but if you can query for the child you want via WP_Query
, then you can filter single_template
to do the redirection based on whether the requested post has a parent or not:
function wpa87313_redirect_post_type( $template ){
// if this isn't the right post type, return
if( ! is_singular( 'my_post_type' ) )
return $template;
// if this post has a parent, return
global $wp_query;
if( 0 != $wp_query->post->post_parent )
return $template;
// query for the child of this parent
// here's where you need to sort out what a first child is
$child = new WP_Query(
array(
'post_parent' => $wp_query->post->ID,
'post_type' => 'my_post_type',
'posts_per_page' => 1,
// orderby?
// order?
)
);
// if a child was found, redirect to it
if( ! empty( $child->posts ) ){
wp_redirect( get_permalink( $child->post->ID ) );
exit;
}
return $template;
}
add_filter( 'single_template', 'wpa87313_redirect_post_type', 10, 1 );
template_redirect
hook but I have no idea what conditions you need to match.