I'd like to add some code from a single post theme file into a filter but I'm not sure how to do it because it includes the get_post_meta() function. I've searched but can't seem to figure it out.

Code in single-post.php theme file:

$apoth_original_title = get_post_meta( get_the_ID(), 'original_blog_title', true);
$apoth_original_url = get_post_meta( get_the_ID(), 'original_url', true);
<a id="apoth_readmore" class="w-btn style_solid size_medium color_primary icon_none" href="<?php echo $apoth_original_url?>"><span class="w-btn-label">Read More at <em><?php echo $apoth_original_title ?></em></span></a>

Attempted filter in functions.php:

function apoth_readmore_link( $content ) {
if( is_single() ) {

    $content .= '<a href="' . get_post_meta(get_the_ID(), 'original_url', true) . '"> Read More</a>' ;
return $content;

add_filter('the_content', 'apoth_readmore_link' )

I get that this isn't working because get_post_meta() doesn't echo the custom field data, but I don't know how to assign that to a variable and then echo it in the context of a filter function. Or maybe there is some other way of doing this that I'm missing?

  • So you would like to modify original_blog_title and original_url from functions.php then render the link with filtered content? – jgraup Nov 16 '16 at 4:55

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="%s">Read More</a>', $url, $url );

    return $content;

add_filter('the_content', 'apoth_readmore_link' );
  • Any luck with this? – jgraup Nov 19 '16 at 17:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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