How to set cookies in WP?

Tell me why I can't set cookies? This is not working.
setcookie('city_name', get_the_title(), time()+3600*24*7);

I found that using add_action('init', function () {}); but the problem is that this function is not working WP_Query. And I need to use cookies to set the value of the name of the page with an extra field. How this problem can be solved?
June 8th 19 at 17:31

Find more questions by tags WordPress