• extravaganzasd

    (@extravaganzasd)


    I am trying to set the post id in a cookie. I am getting the “headers already sent” error if I put it in the header but when I hook it to init in functions file I can’t access the global for post_id.

    function set_new_cookie() {
    
    global $wp_query;
    $post_id = $wp_query->post->ID;	
    
    setcookie("cookie1", $post_id, time()+36000000);
    }
    add_action( 'init', 'set_new_cookie');
    ?>

    Can I put this somewhere where it will work correctly?

The topic ‘Setting the post id in a cookie’ is closed to new replies.