First hook to use current page post id

I'm trying to find the first hook that will pass the current post ID, as I would like to update the current post (b

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.

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.

Share Improve this question edited May 4, 2016 at 12:50 Max Yudin 6,3882 gold badges26 silver badges36 bronze badges asked May 4, 2016 at 12:40 Nabeel KhanNabeel Khan 4085 silver badges17 bronze badges 2
  • Could you elaborate which context you want to do this in? A front page load of singular post? – Rarst Commented May 4, 2016 at 13:09
  • 2 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 Commented May 4, 2016 at 13:16
Add a comment  | 

2 Answers 2

Reset to default 6

Answer by @Pieter Goosen :

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, for example:

function my_early_id() {
    $post = get_post();
    echo $post->ID;
}
add_action( 'wp', 'my_early_id' );

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744842954a4596668.html

相关推荐

  • First hook to use current page post id

    I'm trying to find the first hook that will pass the current post ID, as I would like to update the current post (b

    2天前
    80

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信