I discovered Discord and noticed that its servers are great communities (so much better than Slack’s anyway). One of the technologies that I’m good at is WordPress so I joined an unofficial server for WordPress support.

In there, I noticed that most of the chatter is some guy asking for help to some trivial (for me at least) problem. It’s really simple. So I decided to make it a hobby and help other people. I’ve helped out a few already and some of them offered me jobs or projects but that’s a different story.

How to Show Custom Field Value as Excerpt in WordPress

A guy had a blog with a lot of posts where each of the posts has a custom field with data in it. The data could be used as an excerpt or a short version of the blog post itself.

The Problem

WordPress automatically calculates the excerpt as the first few words of the blog post content. Alternatively, you can enable the Excerpt entry box from the Screen Options pull-down sheet and manually type the excerpt there, and WordPress will automatically use that.

The problem is that transferring the data from the custom field to the excerpt box would take some time since he had lots of blog posts. He also wants this custom field data to show as the excerpt only on search screens.

The Solution

Simple, let’s just use WordPress’ awesome filters feature. I never knew the name of the custom field so let’s just call it MYCUSTOMFIELD.

With a little digging and messing around (hopefully not too messed up though) with your theme’s code, this can be done with the following code being inserted at the end of the functions.php file.

add_filter( 'the_excerpt', function ($original_excerpt) {
    if ( !is_search() ) return $original_excerpt;
    return get_post_meta(get_the_ID(), 'MYCUSTOMFIELD', true);
} );

That should do it!

How to Show Custom Field Value as Excerpt in WordPress

I’d like to believe that my code is self-explanatory if you’re in between being a beginner and intermediate. But if you’re more on the beginner side or there’s something tricky, please don’t hesitate to ask questions.