php – After moving a post to trash, metabox values ​​disappear when I restore the post

Question:

I have a wordpress theme where in the posts I have some custom metabox.

When moving the post to the trash and retrieving it again, it does not return with the metabox values ​​filled in

What can it be?

Answer:

You are experiencing the same problem described in this question (in English).

It's probably a bug in the theme caused because the save_post function is called without the post data when the article is sent to the trash.

As described in the link above, the solution is to add a condition in the save function not to save the metadata in this case.

Example extracted from the link, which you will have to adapt to your theme:

function wpg_save_testimonial_author($post_id) {
  global $post;
  if (!wp_verify_nonce($_POST['testimonial_author_noncename'], plugin_basename(__FILE__).$post->ID)) {
    return $post->ID;
  }
  if(defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
    return $post_id;
  }
  if(defined('DOING_AJAX')) {
    return;
  }
  if(!current_user_can('edit_post')) {
    return $post->ID;
  }
  if($post->post_type == 'revision') {
    return;
  }
  update_post_meta($post->ID, 'testimonial_author_name', $_POST['testimonial_author_name']);
  update_post_meta($post->ID, 'testimonial_author_link', $_POST['testimonial_author_link']);
}
add_action('save_post', 'wpg_save_testimonial_author');
Scroll to Top