Halloween Sale 15% off Ends in See the plans
PHP

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.

Don’t have WPIDE yet? Install it on your site

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

  1. Install WPIDE on your WordPress site (it is free) and open Code Snippets.
  2. Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
  3. It is added inactive: review the code and settings, then switch it on.

Have a snippet worth sharing?

Publish it to the library from WPIDE or right here. Sign in with any WordPress site that runs WPIDE, free or Pro. Public snippets are reviewed by the WPIDE team before they appear.

Which site should get this snippet?

Enter the address of your WordPress site. WPIDE opens there in a new tab with the snippet ready to review and save.

Remembered in this browser only, so you are asked once.

Report this snippet

Tell the WPIDE team what is wrong: broken code, something harmful, spam or copied work.