Add custom field to post with some part of url as value

I found this solution, it works great. I will share it here if someone else need it.

$currenturl="http://" . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
$id = $post->ID;

if ( preg_match('/\?(.+)$/', $currenturl, $matches) ) {
   $value = urldecode($matches[1]);
   add_post_meta($id, 'my_new_custom_field_name', $value, true);
}