Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

share|improve this question
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 '11 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 '11 at 16:02
    
Do you really need to do this on init? Why not later hook? –  Rarst Jul 13 '11 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 '11 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 '11 at 9:26
add comment

1 Answer

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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