2

I have a custom meta field that I want to display as my excerpt. I use a filter that does this for me:

add_filter( 'get_the_excerpt', function($output){
  $output=get_post_meta(get_the_ID(), 'my_meta_field', true);
  return $output;
});

Now whenever I use get_the_excerpt() or the_excerpt() inside of the loop I get the content of my_meta_field.

But since WP 4.5.0 get_the_excerpt() accepts a Post ID or WP_Post object as parameter. I would like to keep this functionality intact while using my filter.

So imagine I want to use get_the_excerpt() outside of the loop. When I call get_the_excerpt(1234) (1234 being the ID of a post) I get the wrong excerpt back because the get_the_ID() in my filter grabs whatever global $post has to offer at that moment.

What is the most elegant / efficient way to solve this? Can I somehow use the ID I am passing to get_the_excerpt inside my filter? Or do I need to create a mini loop and set global $post to get_post(1234)?

  • the problem that I had with the filter the last time I looked was that if you use the_excerpt it is the only one that gets the post id, while the rest of the filters (I think the "more" was the main problem for me) that are being applied do not get it. Maybe I just looked wrong at the code back then, but I advice to test properly the full flow to make sure there are no unexpected surprises. – Mark Kaplun Oct 14 '16 at 8:53
1

In spite of what the Codex says, since WP 4.5, where the addition of the post argument to the function get_the_excerpt was added, this filter takes two arguments. The second argument is the post object whose excerpt you are manipulating.

So the function still works in the loop without an explicit post, we make the second argument optional.

add_filter( 'get_the_excerpt', 'wpse_242462_excerpt_filter' );

function wpse_242462_excerpt_filter( $excerpt, $post = null ){

      if ( $post ) {
        $ID = $post->ID;
      } else {
        $ID = get_the_ID();
      }

      $excerpt = get_post_meta( $ID, 'wpse_242462_meta_field', true);

      return $excerpt;
});

Hopefully it goes without saying that you need to substitute whatever meta key you are already using.

  • Can you elaborate? – Florian Oct 12 '16 at 20:40
  • Just have :-) I was on my phone before, just switched to laptop to type up the code. – Andy Macaulay-Brook Oct 12 '16 at 20:43
  • Hopefully you spotted the error in my code, where I completely missed out the function name! Now corrected. – Andy Macaulay-Brook Oct 12 '16 at 20:51
  • Now when I use get_the_excerpt() inside the loop I get a bunch of warnings and notices and no output. Warning: Missing argument 2, Notice: Undefined variable: post, Notice: Trying to get property of non-object. – Florian Oct 13 '16 at 4:51
  • 1
    Fair point @MarkKaplun, I probably just used whatever is listed in the WP core source comment on the filter without thinking about it. I'll just absorb the hate and diffuse it into code-lurve. – Andy Macaulay-Brook Oct 14 '16 at 9:17
0

Even if you're not in the loop, if you're on within a post or page (or other post type) generated from WordPress, $post will be set. So if you just replace get_the_ID() in your function above with $post->ID, it will work. If you've been running other queries on the page, you may need to run wp_reset_query(); to get the actual post ID.

If you want to pass a specific post, you need to separate the function from the add_filter call along the lines of:

// function to pass the ID to
function my_meta_excerpt($postID){
  $output=get_post_meta($postID, 'my_meta_field', true);
  return $output;
}
// add filter call
add_filter( 'get_the_excerpt', my_meta_excerpt);
  • The point of my question is that I do not want to use the global post. I want to use a post ID of my choice. – Florian Oct 12 '16 at 20:32
  • Let me update my answer – Jonny Perl Oct 12 '16 at 20:35
  • I'll try that! Might take a while though. Thanks! – Florian Oct 12 '16 at 20:41
  • The first argument passed by the filter is an excerpt, not a post ID. – Andy Macaulay-Brook Oct 12 '16 at 20:44

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.