You’re missing the WP $matches[1]
syntax for the regex that gets matched.
I think you want /?
on the end too.
Easiest way I found to test was to temporarily put flush_rewrite_rules()
in whilst testing, then you don’t have to do anything else. I have a similar regex which sends /foo/catname/
to index.php?taxonomy=category&term=catname
, here’s what I used to develop on a clean test WP install – this is working fine for me.
function foobar() {
add_rewrite_rule('^foo/([^/]+)/?', 'index.php?taxonomy=category&term=$matches[1]', 'top');
flush_rewrite_rules();
}
add_action('init', 'foobar', 10, 0);
So you might want to try:
add_rewrite_rule('test-sale-yacht/([^/]+)/?', 'index.php?name=test-sale-yacht&yacht_name_id=$matches[1]', 'top');
Edit: As per comment by @Tom J Nowell, flush_rewrite_rules is an expensive call – my suggestion to do this only on a dev or very low traffic site.