I have a basic function running with the after_setup_theme hook. I need the function to access the current blog post’s id but it’s always false using get_the_ID. How can I access the current post’s id in this function? Code included in the description

[ad_1]

Hi

I have a function that needs to store the current blog’s id in the cookie.

The function I have is this inside **functions.php**:

function set_post_id_in_cookie() {
if (!is_single(‘post’)) return

$post_id = (string) get_the_ID();

return setcookie(‘post_id’, $post_id, time()+86400*30*12, ‘/’);
}

add_action( ‘after_setup_theme’, ‘set_post_id_in_cookie’ );

I have 2 issues:

1. `is_single(‘post’)` always returns false on single posts/blogs. Why?
2. `get_the_ID()` returns `false`. Why?

Any help is appreciated. Thanks

​

[ad_2]

 

This site will teach you how to build a WordPress website for beginners. We will cover everything from installing WordPress to adding pages, posts, and images to your site. You will learn how to customize your site with themes and plugins, as well as how to market your site online.

Buy WordPress Transfer