I want to rewrite the URL of a specific post with a specific custom field to easily analyze in Google Analytics

I would stick to make the url https://example.com/life/life-article/?special=1

I would create a php function that:

  • retrives all posts and stores them in a variable;
  • make a loop to loop all the posts and check if they are “life” posts;
  • inside the same loop, if they are “life” posts, change their slug to …whatever/?special=1;

and then run the function one time somehow.

this should do the trick.
Sorry I dont have posts websites, only products websites, so I do not have a proper code, but this is my idea, and here goes a example code;

function change_life_slugs(){
    $posts = some_function_that_retrieves_all_posts();
    foreach($posts as $post){
       if($post_type->type == "life"){
          wp_update_post([  // this is actually the function to update posts
              "post_name" => "new-slug/?special=1",
              "ID" => $post->id
          ]);
       }
    }
}

this is what I would do, then just put

<?php change_life_slugs(); ?>

on for example front-page.php and go the website main page one time.
after that, once its done, just remove the code from the website. and all “life” would have special equals true on the url.
Sorry for not having the real code, but this the idea design , the functions needed I am sure you find it easily. cheers!