WordPress Excerpt Length for Custom Post Type
If you are a WordPress user, then you know that in WordPress there are many pre-defined functions that automate a lot of the features that dictate how your content on the frontend is displayed. A common example is how your posts loop displays excerpts of your articles — a short summary shown on archive pages, search results, and widgets instead of the full post content.
By default, WordPress trims excerpts to 55 words. But do you know that you can easily customize the WordPress excerpt length — globally, or differently per post type? In this tutorial, we’ll cover changing the excerpt length, applying different lengths to different custom post types, customizing the “Read More” text that follows a trimmed excerpt, and the difference between automatic and manual excerpts that trips up most tutorials on this topic.
What is the Excerpt in WordPress?
In WordPress, an excerpt is a short description or summary of a post that’s shown to readers to grab their attention and make them want to read further. A title alone often isn’t enough to convey what an article is actually about. Excerpts are used to display a post summary on the blog page, search results page, the homepage, and in widgets wherever your posts are listed. Use the the_excerpt() function in a template to display a post’s excerpt.
Automatic vs. Manual Excerpts
WordPress generates an excerpt automatically by trimming post_content to a word count, stripping HTML tags and shortcodes in the process — that’s the “automatic” excerpt this article’s code customizes. If an author fills in the dedicated Excerpt field in the post editor instead, WordPress uses that text verbatim and completely ignores the word-count trimming logic.
Note: This is exactly why the excerpt_length filter shown below has no effect on posts with a manually written excerpt — there’s no trimming happening for WordPress to intercept. If you need a fixed length regardless of manual excerpts too, you’d need to additionally hook get_the_excerpt and run wp_trim_words() on the result yourself.
How to Change the Excerpt Length
To change the excerpt length, add the following code to the functions.php file of your theme.
<?php
/**
* WP Custom Excerpt Length Function
* Place in functions.php
* This example returns ten words, then [...]
* Manual excerpts will override this
*/
function wp_custom_excerpt_length( $length ) {
return 10;
}
add_filter( 'excerpt_length', 'wp_custom_excerpt_length', 999 );
?>
Note: The priority 999 here isn’t arbitrary — some themes and plugins also hook into excerpt_length, and whichever callback runs last wins. Using a very high priority number is a common, if slightly blunt, way to make sure your value is the one that actually takes effect. If your length change doesn’t seem to apply, another callback running at an even higher priority is the first thing to check.
Different Excerpt Lengths per Post Type
You might want to display a different excerpt length depending on the post type — a shorter teaser for a portfolio grid, a longer summary for blog posts. Use get_post_type() inside the filter callback to branch by post type:
<?php
/**
* WP Custom Excerpt Length Function
* Place in functions.php
* Returns a different excerpt length depending on post type
* Manual excerpts will override this
*/
function wp_custom_excerpt_length( $length ) {
$post_type = get_post_type();
if ( $post_type === 'portfolio' ) {
return 15;
}
return 25;
}
add_filter( 'excerpt_length', 'wp_custom_excerpt_length', 999 );
?>
Note: The original version of this snippet used global $post; $post->post_type to check the post type. get_post_type(), used above, is the more current, recommended approach — it works the same way but doesn’t depend on the global $post variable being correctly set in the current scope, which isn’t always guaranteed depending on where the filter fires.
Customizing the “Read More” Text
By default, WordPress appends … (an ellipsis) after a trimmed excerpt. Use the excerpt_more filter to replace it with something more useful, like an actual “Read More” link:
<?php
function wpwebguru_excerpt_more( $more ) {
return '… <a href="' . esc_url( get_permalink() ) . '" class="read-more">' . esc_html__( 'Read More', 'wpwebguru' ) . '</a>';
}
add_filter( 'excerpt_more', 'wpwebguru_excerpt_more' );
?>
Trimming a Custom String to a Word Count Directly
If you need excerpt-style trimming somewhere other than the standard the_excerpt() call — say, trimming a custom field’s text in a template — use WordPress’s own wp_trim_words() function directly instead of reinventing word-counting logic:
<?php echo wp_trim_words( get_post_meta( get_the_ID(), 'subtitle', true ), 12, '…' ); ?>
Common Mistakes to Avoid
- Expecting
excerpt_lengthto affect manually written excerpts — it only affects automatically generated ones, as covered above. - Using a low filter priority when a theme or plugin already hooks the same filter, causing your value to be silently overridden.
- Forgetting
excerpt_moreruns on the automatic excerpt only — manual excerpts won’t get your custom “Read More” text appended either, for the same reason as above.
Wrapping Up
Changing the WordPress excerpt length is a one-filter change, but getting per-post-type behavior and a proper “Read More” link right takes the few extra pieces covered above. If you’re displaying excerpts pulled through a JavaScript front end rather than a PHP template, the same wp_trim_words() approach works well inside a custom REST API endpoint that returns a trimmed summary field alongside the full content.