Get Post ID While Hooking Into template_redirect
-
Hi!
I want to log the ID of the post that has been visited when in single post view. The plugin where I wanna add this uses the “template_redirect” hook.
First, I tried the following:
if(is_single()) { global $post; $post_ID = $post->ID; }
… resulted in a ‘0’ in the relating log table field.
BTW:
That field is called “post_ID” and has the structureBIGINT(20) NULL
.Then, I found something mentioning $wp_query, so I tried:
global $wp_query; if ($wp_query->is_single) { $post_ID = $wp_query->post->ID; } else { $post_ID = NULL; }
Result is the same: ‘0’ in the relating log table field.
Does anyone know what is wrong with the code?
Help would be appreciated.
Thanks!EDIT:
As I just found out, the is_single() should already work at template_redirect, see this source. So, it all comes down to the $post or $wp_query class, right?
Viewing 4 replies - 1 through 4 (of 4 total)
Viewing 4 replies - 1 through 4 (of 4 total)
- The topic ‘Get Post ID While Hooking Into template_redirect’ is closed to new replies.