It seems that $post is only available to hooks executing at certain times. I have an “init” hook that needs to pull some data from the database using the $post->ID.
So far my only workaround has been to add another filter on the_content, that uses $post to get the information I need. Unfortunately, it also echoes the return value to the screen, and fails to actually return it to the function that called the filter originally.
Here is the code that extracts the data I need, but echoes and fails to return the value:
add_filter('the_content', 'get_keyword');
function get_keyword()
{
global $post;
$keyword = get_post_meta( $post->ID, '_wpg_def_keyword', true );
return $keyword;
}
Does anyone have any suggestion on how to get what I need and have it pass back to the calling function for later use in the plugin?
EDIT: To possibly make this more clear, I have a filter running at init, that needs to be able to retrieve information on the current post from the DB. To do this, it requires access to $post->ID. But that is not possible within init, so how can I get the result I need?
Thanks,
Jonathan
SOLVED:
Turns out the answer was simply to use url_to_postid like this:
$keyword = get_post_meta(
url_to_postid( "http://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'] ),
'_wpg_def_keyword',
true
);
Works perfectly even from init.