I need a hook such that when visitor lands on any of the post (not page), I need to record a hit.

How can I find whether the request is for post, and is there any hook to find if the loaded content is post, and if yes, what is post id

1 Answer 1


something like this will work:

function record_hit_if_post(){
    global $wp_query;
        // single post's ID:
        // $wp_query->post->ID;
add_filter('template_redirect', 'record_hit_if_post');
  • is_single might come up with page too, don't you think so?
    – I-M-JM
    Commented May 3, 2011 at 2:22
  • 1
    no, just single post. is_page == 1 on a page
    – Milo
    Commented May 3, 2011 at 3:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.