Fill the Excerpt From an ACF Summary Field
Copies an ACF summary field into the post excerpt on save when the excerpt is empty, so themes, feeds and search results show your summary.
Opens WPIDE on
Code
// Summary field and the post types it applies to.
$wpide_acf_excerpt_field = 'summary';
$wpide_acf_excerpt_types = array( 'post' );
add_action( 'acf/save_post', function ( $post_id ) use ( $wpide_acf_excerpt_field, $wpide_acf_excerpt_types ) {
if ( ! is_numeric( $post_id ) || wp_is_post_revision( $post_id ) || ! in_array( get_post_type( $post_id ), $wpide_acf_excerpt_types, true ) || '' !== trim( (string) get_post_field( 'post_excerpt', $post_id ) ) ) {
return;
}
$summary = sanitize_textarea_field( (string) get_post_meta( $post_id, $wpide_acf_excerpt_field, true ) );
if ( '' !== $summary ) {
wp_update_post( array( 'ID' => (int) $post_id, 'post_excerpt' => $summary ) );
}
}, 20 );
Notes
Set the field name and post types. Excerpts you write by hand are kept.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.