Change the Default Excerpt Length in WordPress

WordPress cuts automatic excerpts at 55 words. On blog cards, archive grids and search results that is often too long for the design. This snippet changes the WordPress default excerpt length to the number of words you want, for every post type at once or with a different length per post type.

The snippet

<?php
/**
 * Change the WordPress excerpt length (number of words in automatic excerpts).
 */

// ===== Config: words per automatic excerpt (WordPress default is 55) =====
function dpc_excerpt_length_config() {
	$config = array(
		'default' => 15,
		// 'product' => 30, // Per post type, e.g. longer excerpts for products.
		// 'page'    => 25,
	);
	// ===== End of config =====

	return (array) apply_filters( 'dpc_excerpt_length_config', $config );
}

// Remember which post the excerpt is being built for; excerpt_length itself does not get it.
add_filter( 'get_the_excerpt', 'dpc_excerpt_length_remember_post', 9, 2 );

function dpc_excerpt_length_remember_post( $excerpt, $post = null ) {
	$GLOBALS['dpc_excerpt_length_post'] = $post;
	return $excerpt;
}

add_filter( 'excerpt_length', 'dpc_excerpt_length', 999 );

function dpc_excerpt_length( $length ) {
	$config    = dpc_excerpt_length_config();
	$post_type = get_post_type( $GLOBALS['dpc_excerpt_length_post'] ?? null );

	if ( $post_type && isset( $config[ $post_type ] ) ) {
		return max( 1, absint( $config[ $post_type ] ) );
	}
	if ( isset( $config['default'] ) ) {
		return max( 1, absint( $config['default'] ) );
	}

	return $length;
}

How it works

  • wp_trim_excerpt() builds an excerpt from the post content when the post has no hand-written excerpt, and asks the excerpt_length filter how many words to keep. The snippet answers with the number from the config, 15 by default.
  • The filter runs at priority 999, so a theme or plugin that sets its own length at the default priority does not override yours.
  • excerpt_length does not say which post it is trimming. The small get_the_excerpt callback at priority 9 remembers the post just before WordPress trims it, so a per post type length also works outside the loop, for example in a widget or a custom query.
  • Values are passed through absint() and kept at 1 or more, so a typo cannot produce an empty excerpt.

Add it with Scripts Organizer

In Scripts Organizer go to Code Blocks → Add New and give the block a name. Set Trigger location to Everywhere, tick PHP under Script location, paste the code into the PHP editor, edit the config and publish. Leave Action/Hook empty. Everywhere matches where excerpts appear: front-end templates, RSS feeds and the REST API.

The same code also works unchanged in a small plugin or an mu-plugin.

Customise it

  • Different length: change 'default' => 15 to the number of words you want.
  • Per post type: uncomment a line such as 'product' => 30, and use the post type slug. Post types that are not listed use default.
  • From another plugin: add a filter to dpc_excerpt_length_config and change the array there.
  • To change the […] at the end of the excerpt, see [Change the WordPress Excerpt […] to a Read More Link](https://code.dplugins.com/change-default-excerpt/).

Gotchas

  • Only automatic excerpts are trimmed. A hand-written excerpt in the post’s Excerpt box is shown as written, whatever its length.
  • In block themes the Post Excerpt block has its own Max number of words setting, and that setting wins. Change it in the Site Editor instead.
  • Some themes and page builders trim excerpts with their own code instead of excerpt_length. If nothing changes, check your theme’s excerpt setting first.
  • Words are counted by spaces. For Chinese, Japanese and similar languages WordPress counts characters instead, depending on the site language.

The older version of this post set 20 words with an anonymous function at the default priority. The Scripts Organizer library version used 15 words at priority 999; this version keeps that and adds the per post type option.

In the Scripts Organizer library Import it with one click from Scripts Organizer.

// newsletter

Three tested WordPress snippets a week

Blocks, security, performance, Tailwind, PHP and React. Copy, paste, ship. You’ll also get DPlugins product updates. No spam, and you can unsubscribe anytime.

// newsletter

Three tested WordPress snippets a week

Blocks, security, performance, Tailwind, PHP and React. Copy, paste, ship. You'll also get DPlugins product updates. No spam, and you can unsubscribe anytime.