The hook before_delete_post
fires before postmeta is deleted. That explains why it’s not isolating the condition you want. Another part of the problem is you’re trying to add postmeta while it is being deleted – it’s not clear why you need published_send_once
but you’ll have to find another way to save that information than adding postmeta at the time all postmeta is deleted. Side note, posts of type “revision” cannot have the status “publish” – revisions always have a status of “inherit.”
When a post is deleted, the hooks transition_post_status
, post_updated
, and save_post
are all run. transition_post_status
runs first of the three, so I’d suggest using it to trigger your notification:
// hook to transition_post_status
add_action('transition_post_status', 'wpse_test', 10, 3);
// the hook gives you the new status, old status, and post object
function wpse_test($new_status, $old_status, $post) {
// only run when the status changes from publish to trash
// you might also want to only run when the post_type is attachment
if($new_status == 'trash' && $old_status == 'publish') {
// verify current user is an Administrator - by capability, not role
if(current_user_can('update_core')) {
// send notification
$author = $post->post_author;
$author_name = get_the_author_meta( 'display_name', $author );
$link = 'https://www.cgartzone.com/submission-rules';
$image="https://www.cgartzone.com/wp-content/themes/hitmag-child/assets/images/sad-icon-01.svg";
$title="<strong>".$author_name.'</strong> ' . __('We are sorry to inform you that your artwork has been deleted. We recommended you to re-read our rules or contact us for more informations', 'dw-notifications'). ' <strong>'.$post->post_title.'</strong>';
$notif = array('title'=>$title, 'link' => $link, 'image' => $image);
dwnotif_add_notification($author, $notif);
}
}
}
Using this other hook will allow you to verify that your code only runs when a publish
status post switches to trash
status.