I’m writing a plugin and am trying to figure out how to add data to the end of a post based on some Custom Meta Information.
So i’ve done this so far:
add_filter('the_content', 'AppendMeta');
And here is the “AppendMeta” function
AppendMeta($content) {
echo $content; // Echo out post content
$PersonName = get_post_meta($post->ID, 'PersonName', true);
echo 'Person: ' . $PersonName;
}
That code works if I replace $post->ID with the id of the post, but I need it to work based on the post the user is currently navigating. How would I pass the post id in as a parameter? $post->ID doesn’t work in this scenario and I can’t find out why.
Nevermind, found out I can use
get_the_ID();
.This function will return the post id inside the
the_content
filter. The function simply declares the global$post
object and returns its ID.If you don’t want to use
get_the_ID()
, you simply need to declare the$post
object global before using it:the_ID()
is only supposed to be used inside the loop, else you can’t rely on it. Better use the$GLOBALS['post']
object inside your function call.