-1
function add_custom_fields($post_id) {
    global $post;

    $metadescription = wp_trim_words( get_the_content(), 55 );
    add_post_meta( $post_id, 'meta_description', $metadescription, true );
}
    
add_action('wp_insert_post', 'add_custom_fields');

1 Answer 1

1

You need to pass the $post_id into the get_the_content() :

function add_custom_fields($post_id) {
    global $post;

    $metadescription = wp_trim_words( get_the_content($post_id), 55 );
    add_post_meta($post_id, 'meta_description', '' . $metadescription . '', true);
}
    
add_action('wp_insert_post', 'add_custom_fields');
1
  • output is blank
    – Bharat
    Jan 12 at 13:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.