• Hi, I think, that you should do like this via post_password_required.

    
    add_action( 'template_redirect', 'test_template_redirect', 10 );
    function test_template_redirect() {
    
        $post_id = get_the_ID();
        $parent_ids = get_post_ancestors( $post_id );
    
        if ( ! $parent_ids || empty( $parent_ids ) ) {
            return;
        }
    
        ksort( $parent_ids );
    
        foreach ( $parent_ids as $parent_id ) {
            // better would be detect parent of post and redirect, but for my personal purpouse I need to redirect to top parent post.
            if ( post_password_required( $parent_id ) ) {
                wp_redirect( get_the_permalink( $parent_ids[0] ) );
                exit();
            }
        }
    };
  • The topic ‘better solution’ is closed to new replies.