get_post() is not returning correct value

That’s not how you update posts. Because you used raw SQL to do it, none of the filters or actions fired, and none of WP_Cache was updated, so it’ll return an old version of the post it fetched earlier when you call get_post.

Instead, if you want to update a post, use wp_insert_post and pass the posts ID parameter.

E.g.

$result = wp_insert_post( [
    'ID' => 84887,
    'post_parent' => 370,
] );

Additional notes:

  • Don’t use raw SQL to do things if there’s an API ( for this there were several )
  • Don’t hardcode post IDs, use slugs if you must hardcode anything. All it takes is an accidental post deletion and the code has to be changed at least with slugs you can rename a post and it works again ( and it won’t work if you migrate the site with hardcoded numbers )
  • numbers aren’t strings, use 5 not '5' especially when using SQL functions