Getting 404 error while rendering question page.

4.87K viewsIssues
0

Hi,

I am facing an issue on rendering question page. If i set the post parent for any question and then try to render that page,  its gives me an 404 error.  If I removed the parent id  then its work fine.

Please suggest me how to fix this issue.

0

Thanks for your reply Rahul.

I tried to flush the rewrite rule after new question hook where I am changing the parent Id for the question, but its not working for me.  I got the 404 error.

In rewrite.php there is following function:

public static function add_query_var($wp) {
    if ( ! empty( $wp->query_vars['question_name'] ) ) {
       $wp->set_query_var( 'ap_page', 'question' );
        $question = get_page_by_path( sanitize_title( $wp->query_vars['question_name'] ), 'OBJECT', 'question' );
     

        if ( $question ) {
           $wp->set_query_var( 'question_id', $question->ID );
        }
        // Rediret to 404 page if question does not exists.
        else {
           global $wp_query;
         $wp_query->set_404();
         status_header( 404 );
         get_template_part( 404 );
         exit();
        }
    }
    if ( ! empty( $wp->query_vars['ap_user'] ) ) {
           $user = get_user_by( 'login', sanitize_text_field( urldecode( $wp->query_vars['ap_user'] ) ) );

           if ( $user ) {
         $wp->set_query_var( 'ap_user_id', $user->ID );
      }
    }
}

In this get_page_by_path return null, if I set the parent id for the question.

0

Hello Prashant,

Make sure to flush rewrite rules after changing parent ID.