J'essaie de supprimer un message avec toutes ses pièces jointes. C’est la fonction que j’ai imaginée pour le moment;
function remove_post(){
if(isset($_POST['post_id']) && is_numeric($_POST['post_id'])){
$post = get_post($_POST['post_id']);
if(get_current_user_id() == $post->post_author){ echo 'ee';
wp_delete_post($_POST['post_id']);
}
}
exit;
}
Ceci supprime la publication mais pas les pièces jointes. La publication reste dans la corbeille. Alors qu'est-ce que tu en penses? Merci.
Peut-être que cela fonctionne
function remove_post() {
if(isset($_POST['post_id']) && is_numeric($_POST['post_id'])) {
$post = get_post($_POST['post_id']);
if(get_current_user_id() == $post->post_author) {
$args = array(
'post_parent' => $_POST['post_id']
);
$post_attachments = get_children($args);
if($post_attachments) {
foreach ($post_attachments as $attachment) {
wp_delete_attachment($attachment->ID, true);
}
}
wp_delete_post($_POST['post_id'], true);
}
}
exit;
}
Le code ajouté
function get_attachment_id_from_src ($image_src) {
global $wpdb;
$query = "SELECT ID FROM {$wpdb->posts} WHERE guid='$image_src'";
$id = $wpdb->get_var($query);
return $id;
}
if(!empty($_POST['avatar_id']) && $_POST['avatar_id'] != get_user_meta($current_user->id, 'custom_avatar', true) && empty( $_POST['remove_avatar'])) {
update_user_meta($current_user->id, 'custom_avatar', esc_attr( $_POST['avatar_id']));
} elseif(!empty( $_POST['remove_avatar']) && $_POST['remove_avatar'] == 1) {
$avatar_url = get_user_meta($current_user->id, 'custom_avatar', true);
$attachment_delete = get_attachment_id_from_src($avatar_url);
wp_delete_attachment($attachment_delete, true);
update_user_meta( $current_user->id, 'custom_avatar', '-1');
}
Voir le codex pour l'utilisation correcte de wp_delete_post , use
wp_delete_post( $_POST['id'], true );
contourner la corbeille