Using custom fields in a filter hook

Try get_queried_object() to reference the WP_Post that created the page.

  • if you’re on a single post, it will return the post object
  • if you’re on a page, it will return the page object
  • if you’re on an archive page, it will return the post type object
  • if you’re on a category archive, it will return the category object
  • if you’re on an author archive, it will return the author object
  • etc.
function apoth_readmore_link( $content ) {

    $post = get_queried_object();

    if ( get_class($post)==='WP_Post' ) {

        $url = get_post_meta($post->ID, 'original_url', true);

        $content .= sprintf( '<a href="https://wordpress.stackexchange.com/questions/246324/%s">Read More</a>', $url, $url );
    }

    return $content;
}

add_filter('the_content', 'apoth_readmore_link' );

Leave a Comment