Save custom meta-box after excerpt saves

156 views Asked by At

I want this custom field to use the excerpt if it is left blank. However, it appears as though the excerpt is getting saved after this function runs (it's always 1 'publish' click behind).

Any idea how to run this function after the excerpt is saved? Or is it running at the correct time and I'm just doing something wrong?

function save_seo_meta($post_id) {
  $seo_desc = (isset($_POST['seo-desc']) && $_POST['seo-desc']) ? $_POST['seo-desc'] : get_the_excerpt();
  update_post_meta($post_id, '_seo-desc', $seo_desc);
}
add_action('save_post', 'save_seo_meta');
1

There are 1 answers

0
Rice_Crisp On BEST ANSWER

Simple solution: just use the post data

function save_seo_meta($post_id) {
  $seo_desc = (isset($_POST['seo-desc']) && $_POST['seo-desc']) ? $_POST['seo-desc'] : $_POST['excerpt'];
  update_post_meta($post_id, '_seo-desc', $seo_desc);
}
add_action('save_post', 'save_seo_meta');