WordPress Delete Post

WordPress permette di intercettare quando un post – anche di tipo custom – sta per essere eliminato. Esistono due hook action che possono essere utilizzate: delete_post e deleted_post. In prima analisi la prima dovrebbe essere riconducibile ad un “sta per essere eliminato” il post, anche se così non sembra essere, o meglio. Analizzando il codice si evince infatti che i due hook sono praticamente chiamati in contemporanea:

1
2
3
4
5
...
do_action( 'delete_post', $postid );
$wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->posts WHERE ID = %d", $postid ));
do_action( 'deleted_post', $postid );
...

Ma, cosa più importante, è che alcune informazioni al post correlate, come meta data, commenti o post di tipo attachment, sono già state rimosse a quel punto dell’esecuzione. Quindi se scriviamo un codice di questo tipo, esso non funzionerà:

1
2
3
4
5
6
7
8
9
add_action('delete_post', my_delete_post);
...
function my_delete_post($post_id) {
  global $wpdb;
  // Se proviamo a cercare il nostro post, già non lo troviamo più
  $sql = sprintf("SELECT ID FROM `%s` WHERE `post_parent` = %s AND `post_type` = 'attachment'", $wpdb->posts, $post_id);
  $attachmentID = $wpdb->get_var($sql);
  // restituire null
}

Attenzione, un attachment non è eliminato, viene eliminato il suo collegamento con il post.
Quello che ho scoperto, comunque, è che esiste un before_delete_post (e anche un after_delete_post) che svolge meglio la funzione. Questo perché se scriviamo:

1
2
3
4
5
6
7
8
9
add_action('before_delete_post', my_delete_post);
...
function my_delete_post($post_id) {
  global $wpdb;
  // Se proviamo a cercare il nostro post, già non lo troviamo più
  $sql = sprintf("SELECT ID FROM `%s` WHERE `post_parent` = %s AND `post_type` = 'attachment'", $wpdb->posts, $post_id);
  $attachmentID = $wpdb->get_var($sql);
  // restituire un ID valido - se esiste
}

Buona norma, nel coso di presenza di post custom, sarebbe quella di scrivere un codice di questo tipo:

1
2
3
4
5
6
7
8
9
10
add_action('before_delete_post', my_delete_post);
...
function my_delete_post($post_id) {
  global $wpdb;

  $post = get_post($post_id);
  if ($post->post_type == 'custom_type') {
    // Al post di 'custom_type' è possibile usare post, attachment, page, ...
  }
}

Non ci sono commenti per questo Post

Lascia un commento

TAG XHTML PERMESSI: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> INSERIMENTO CODICE:
<pre></pre> // blocco generico
                   <code></code> // blocco generico
                   [cc_actionscript][/cc_actionscript] // Actionscript
                   [cc_actionscript3][/cc_actionscript3] // Actionscript 3
                   [cc_css][/cc_css] // CSS Style Sheet
                   [cc_html][/cc_html] // HTML
                   [cc_js][/cc_js] // Javascript
                   [cc_objc][/cc_objc] // Objective-C
                   [cc_php][/cc_objc] // PHP
                   [cc_sql][/cc_sql] // SQL