0

When the user chooses to restore a post ( from "Trash" folder ) with the following way, through the action untrash_post, i get the id of the posts.

function my_untrash_post_type( $post_id )
{
    echo '<script> alert('.$post_id.'); </script>';
    wp_die('stop');
}

add_action('untrash_post' , 'my_untrash_post_type');

But if the user chooses more than one, posts to restore them, i cannot find the way in which will be able to get all the ids of the selected posts. How could i do that?

0

You can use transition_post_status:

add_action('transition_post_status', 'wpse_handle_untrash');
function wpse_handle_untrash($new_status, $old_status, $post) {
    // if the post was in the trash, but now is not
    if($old_status == 'trash') {
        // if you want, you can do something only for a certain post type
        if($post->post_type == 'my-desired-post-type') {
            // do something
            echo '<script>alert(' . $post->ID . ');</script>';
        }
    }
}

If you can explain what you'd like to do when a post is untrashed, that will be helpful in getting you a solution to accomplish it. Is a JS alert what you're ultimately hoping to achieve?

This question may give you some ideas.

  • First of all you mean : if($post->post_type == 'my-desired-post-type') , right? Thank you very much for your (quick) answer, but it didn't helped me in my problem. Because i want to know, after the action ( "restore" - untrash ) be completed, which posts have been restored. The code i have done so far and is not working is the here. Thanks you. – Shake-the-World Jul 11 '17 at 19:07
  • The code in this answer runs when a post status changes from being in the trash to not being in the trash. It contains the $post object so you can pull the ID from that post object. Since this code runs on any post status transition, the conditional for $old_status == 'trash' ensures it only runs when something is being moved out of the trash. – WebElaine Jul 11 '17 at 20:18
  • Yes, i agree with that. Thanks you. But you've seen my code above ( in Pastebin ) ? What i really want is, if more than one post is selected, to be restored, after as soon as this happens, i display their ids. In my code, i use the redirect_post_location WordPress filter. – Shake-the-World Jul 12 '17 at 9:44
  • Any help about this problem? – Shake-the-World Jul 24 '17 at 17:13

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.