How to get Post ID with the Add Filter Function

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.

add_filter('the_content', 'wpse51205_content')
wpse51205_content($content) {
    echo $content; // Echo out post content
    $PersonName = get_post_meta(get_the_ID(), 'PersonName', true);
    echo 'Person: ' . $PersonName;
}

If you don’t want to use get_the_ID(), you simply need to declare the $post object global before using it:

add_filter('the_content', 'wpse51205_content')
wpse51205_content($content) {
    global $post;
    echo $content; // Echo out post content
    $PersonName = get_post_meta($post->ID), 'PersonName', true);
    echo 'Person: ' . $PersonName;
}

Leave a Comment

tech