Title: better solution
Last modified: October 29, 2020

---

# better solution

 *  [Hrohh](https://wordpress.org/support/users/hrohh/)
 * (@hrohh)
 * [5 years, 7 months ago](https://wordpress.org/support/topic/better-solution/)
 * 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.

 * ![](https://ps.w.org/protect-the-children/assets/icon-256x256.jpg?rev=1701934)
 * [Protect the Children!](https://wordpress.org/plugins/protect-the-children/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/protect-the-children/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/protect-the-children/)
 * [Active Topics](https://wordpress.org/support/plugin/protect-the-children/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/protect-the-children/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/protect-the-children/reviews/)

 * 0 replies
 * 1 participant
 * Last reply from: [Hrohh](https://wordpress.org/support/users/hrohh/)
 * Last activity: [5 years, 7 months ago](https://wordpress.org/support/topic/better-solution/)
 * Status: not resolved