I'm trying to find the first hook that will pass the current post ID, as I would like to update the current post (by getting its ID) and the variables submitted to that page.

This is the code I'm using:

class my_class {

    public function __construct(){

        add_action( 'init', array( $this, 'my_method' ) );

    public function my_method( $atts ){

        // do my stuff with current page id here.
        // even just: echo post id and die();.


Any hook, action or filter, and which one to use and why.

  • Could you elaborate which context you want to do this in? A front page load of singular post? – Rarst May 4 '16 at 13:09
  • 1
    If you need to update the page before the main query fires and returns the page object, you will manually need to parse the URL (probably on init) and get the page ID from get_page_by_title() or get_page_by_path(). Otherwise, wp would be earliest hook to get the page ID – Pieter Goosen May 4 '16 at 13:16

I think you are looking for the_post (see action reference.). That is the first hook within the loop.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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