Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm using the trash_post hook to set a flag in a custom table to indicate that this item is "deleted", but when the user chooses to restore that post, what hook can I use for that? I couldn't find anything on this page https://codex.wordpress.org/Plugin_API/Action_Reference, but maybe there's another way to solve the problem.

Thanks!

share|improve this question
add comment

3 Answers

up vote 3 down vote accepted

Looking at the code for WP 3.3.2, it seems that trash_post is actually wp_trash_post. From the wp_trash_post() function in /wp-includes/post.php:

do_action('wp_trash_post', $post_id);

add_post_meta($post_id,'_wp_trash_meta_status', $post['post_status']);
add_post_meta($post_id,'_wp_trash_meta_time', time());

$post['post_status'] = 'trash';
wp_insert_post($post);

wp_trash_post_comments($post_id);

do_action('trashed_post', $post_id);

So ... I'd double check the hook you're using to set your initial flag.

However, there is a hook you can use to detect when a user restores a post. It's aptly named untrash_post.

Here it is in action from the same core file:

function wp_untrash_post($post_id = 0) {
    if ( !$post = wp_get_single_post($post_id, ARRAY_A) )
        return $post;

    if ( $post['post_status'] != 'trash' )
        return false;

    do_action('untrash_post', $post_id);

    $post_status = get_post_meta($post_id, '_wp_trash_meta_status', true);

    $post['post_status'] = $post_status;

    delete_post_meta($post_id, '_wp_trash_meta_status');
    delete_post_meta($post_id, '_wp_trash_meta_time');

    wp_insert_post($post);

    wp_untrash_post_comments($post_id);

    do_action('untrashed_post', $post_id);

    return $post;
}
share|improve this answer
    
trash_post does work, but do you recommend I use wp_trash_post instead? I found it in sitepress.class.php: add_action('trash_post', array($this,'trash_post_actions')); –  NightHawk May 4 '12 at 21:11
    
I can't speak for anything in sitepress.class.php since it's part of a premium plugin that I don't have. But I did do a fresh download of WP 3.3.2, and there is no action called trash_post in core. It's possible that WPML (the plugin that includes sitepress.class.php) defines the trash_post hook as a wrapper for wp_trash_post. –  EAMann May 4 '12 at 21:17
    
Actually, it looks like trash_post was changed to wp_trash_post for WP 3.3 (core.trac.wordpress.org/changeset/18974), so add your action to both hooks to make sure it works with 3.3 and later. –  EAMann May 4 '12 at 21:20
    
Yes, you are right about. The repository I searched had trash_post in that plugin, however the install that I tested trash_post on did not have this plugin (it was a new install) and trash_post did work. This doesn't make any sense though. –  NightHawk May 4 '12 at 21:20
    
See my last comment (was editing when you posted). WP renamed the hook in 3.3 ... Seems when you change a post status to "trash" it calls a dynamic hook post-status_post-type which evaluates to trash_post ... they renamed the hook to avoid collisions. So your code will still work ... only you're tying to a completely different hook than you thought :) –  EAMann May 4 '12 at 21:21
show 5 more comments

When a post is untrashed, WordPress calls wp_untrash_post. That calls two actions: untrash_post prior to untrashing and untrashed_post after. Both pass the post ID as an argument.

share|improve this answer
add comment

Somewhere on the edit.php is a wp_untrash_post but the documentation page is empty (http://codex.wordpress.org/Function_Reference/wp_untrash_post), maybe if you take a look to the source you may be able to find a way http://wpseek.com/wp_untrash_post/ http://phpxref.ftwr.co.uk/wordpress/nav.html?wp-admin/edit.php.source.html

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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