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?




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'] ),

Works perfectly even from init.

  • 1
    you need an action hook and not a filter hook, also init is too early for global $post; to be in the scope. maybe if you describe what you are trying to accomplish will help better.
    – Bainternet
    Jul 13, 2011 at 8:35
  • action hook behaves exactly the same way, I've tried both. I know init is too early, I'm trying to figure out how to accomplish the result I need - which is a filter running at init receiving information from the DB that requires $post-ID to retrieve.
    – Jonathan
    Jul 13, 2011 at 16:02
  • Do you really need to do this on init? Why not later hook?
    – Rarst
    Jul 13, 2011 at 19:08
  • I have to retrieve the default keyword for the page, if none has been provided in the URL already. That keyword is stored in the DB, and must be known before any of the rest of the script executes. As you can see in the sample I provided, I'm only able to get the value I need in the_content, but then it echoes to the screen and does not pass back to the calling function at all. If you have another solution, please suggest it.
    – Jonathan
    Jul 13, 2011 at 19:47
  • Please move your solution to an answer, tha would be more in line with site's mechanics. Also no need to put solved in title - you would be able to accept answer and your question would be marked as answered.
    – Rarst
    Jul 15, 2011 at 9:26

3 Answers 3


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.


Had problems using the accepted answer as it does not work with ports and multisite ports. Here is a solution which should work in every case:

* Note: This function will only work on SINGULAR posts/pages/post types
function get_early_postid() {
    return url_to_postid((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]");

// demo

function my_super_early_meta_action() {
    $keyword = get_post_meta( get_early_postid(), '_wpg_def_keyword', true );

This also works in WordPress 4.7:

$postID = url_to_postid( $_SERVER['REQUEST_URI'] , '_wpg_def_keyword', true ); 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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