WordPress.org

Support

Support » Requests and Feedback » `$post->ID` not accessible within a filter hook

`$post->ID` not accessible within a filter hook

  • I am currently implementing a filter hook based on the_content hook. I need to retrieved the $post->ID, unfortunately this value is not accessible (or not set already) within the hook call.

    Although I noticed that calling the_ID is working. But for my own regex purpose I need to actually retrieve the ID value rather that simply echo it.

    For now, I am using global $id to access the current post ID. But I feel this is not a clean approach since $id is not supposed to be directly accessed that way.

    What would you suggest as a clean approach to this problem?

Viewing 1 replies (of 1 total)
  • In a few of my plugins I just set $wp_query to global, then access the ID (and other elements) of a post like so:

    global $wp_query;
    $post = $wp_query->post;

Viewing 1 replies (of 1 total)
  • The topic ‘`$post->ID` not accessible within a filter hook’ is closed to new replies.
Skip to toolbar