hooks - get $post in init filter or action?

admin2025-06-04  0

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.

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 edited Sep 15, 2017 at 21:46 Ivijan Stefan Stipić 4553 silver badges16 bronze badges asked Jul 13, 2011 at 7:45 JonathanJonathan 1511 gold badge1 silver badge4 bronze badges 5
  • 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 Commented 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 Commented Jul 13, 2011 at 16:02
  • Do you really need to do this on init? Why not later hook? – Rarst Commented 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 Commented 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 Commented Jul 15, 2011 at 9:26
Add a comment  | 

3 Answers 3

Reset to default 16

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
add_action('init','my_super_early_meta_action');

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 ); 
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748997247a315482.html

最新回复(0)