1

I have some metadata on my post edit page that saves perfectly using the hook and function below:

    add_action('save_post', 'save_meta_data');
    function save_meta_data($post_id){
      if(isset($_POST['target_url'])) {
        update_post_meta($post_id, 'target_url', $_POST['target_url']);
      }
    };

However, the same logic fails to save the meta data before a post is trashed which is what that hook is for (so I thought).

    add_action('wp_trash_post', 'save_meta_data');
    function save_meta_data($post_id){
      if(isset($_POST['target_url'])) {
        update_post_meta($post_id, 'target_url', $_POST['target_url']);
      }
    };

It looks like in the second case that the whole $_POST array is empty, but I know the function is firing.

Could some kind soul point out what am I doing wrong and what is the proper way to do this?

Thanks, Ron

1 Answer 1

1

The Core "trash post" links in the "Quick Edit" section on edit.php and in the "advanced" form in the "Publish" meta box work over GET not POST. Unless you have altered the form(s) somehow, there is no POST data. All you have is the post ID.

To save data when a post is deleted, you won't be able to use the default "delete/trash" functionality. You will need to:

  1. Save your post with the (I assume) custom meta field "delete/trash" description, then trash the post.
  2. Submit your "delete/trash" description via AJAX either when you fill out the description or when you hit the "delete/trash" link.
  3. Alter the form so that the "delete/trash" link submits the whole form over POST
  4. Remove the "delete/trash" links and replace them with a checkbox so that you can submit the form as normal and if the "delete/trash" checkbox is checked a filter on save_post can do the actual delete.

Off the top of my head, I can't tell you how hard modifying the form will be, but it won't likely be simple. The AJAX options are probably easier, but can easily be disabled by switching off Javascript in the browser.

1
  • That makes sense. So what would you do in this case to save any changes to the meta data before the post is trashed? I'm using "trash" as a valid status, and want to preserve those last-second changes. The data I'm actually saving is a text message about why the post was trashed, but the example I gave was simpler.
    – Ron
    Jul 23, 2013 at 16:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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