`$post->ID` not accessible within a filter hook (2 posts)

  1. JoannesVermorel
    Posted 11 years ago #

    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?

  2. Kafkaesqui

    Posted 11 years ago #

    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;

Topic Closed

This topic has been closed to new replies.

About this Topic