The problem is in your rewrite rule:
add_action( 'init', 'my_add_rewrite_rules' );
function my_add_rewrite_rules() {
add_rewrite_rule('^child/([^/]+)/?','index.php?post_type=child&post_parent=$post_id','top');
}
Instead of using the matched value, you’ve used $post_id
. Since this isn’t a double quoted string, and there is no $post_id
variable, the value of post_parent
will be the string literal "$post_id"
, but post_parent
expects a numeric post ID
Instead, refer to the official documentation which uses this example, and even has a safer regular expression:
add_rewrite_rule('^leaf/([0-9]+)/?', 'index.php?page_id=$matches[1]', 'top');
So no, you don’t need to flush the value because there is nothing to flush, that’s not how rewrite rules work.
Finally, post_parent
is already a query var, you don’t need to add it a second time.