utilizzare wp_update_post su save_post
9 nov 2011, 23:14:33
Visualizzazioni: 4.75K
Voti: 5
Sto cercando di aggiornare la data di un post (-1 anno) quando si preme aggiorna, ma causa un loop infinito.
Ci sono altri modi per farlo?
Grazie.
function cambia_anno($post_id, $post){
if ( $post->post_type == 'post' ) {
$format = 'Y-m-d H:i:s';
$id = $post->ID;
$old_date = $post->post_date;
$old_gmt_date = $post->post_date_gmt;
$new_date = date( $format, strtotime( '-1 year' , strtotime( $old_date ) ) );
$new_date_gmt = date( $format, strtotime( '-1 year' , strtotime( $old_gmt_date ) ) );
$new_values = array (
'ID' => $id,
'post_date' => $new_date,
'post_date_gmt' => $new_date_gmt
);
wp_update_post( $new_values );
}
}
add_filter('save_post', 'cambia_anno',10,2);

chrismccoy
309
Commenti
Tutte le risposte alla domanda
1
2
Il motivo per cui diventa infinito è che ogni volta che salvi il post, viene chiamata la funzione change_year
... che a sua volta chiama wp_update_post
... che attiva il filtro save_post
.
Dopo alcune verifiche e ricerche, penso che probabilmente dovresti evitare il filtro save_post
.
Prova a utilizzare questo filtro: http://codex.wordpress.org/Plugin_API/Filter_Reference/wp_insert_post_data
Ti dà esattamente ciò che desideri.
Ecco un esempio di come modificare i dati pubblicati:
function filter_handler( $data , $postarr ) {
$data[ 'post_title' ] = $postarr[ 'post_title' ] . 'RAWR!';
return $data;
}
add_filter( 'wp_insert_post_data' , 'filter_handler' , '99', 2 );
Questo codice prenderà qualsiasi post che salvo e aggiungerà 'RAWR!' alla fine della stringa.
Spero sia utile.

Sterling Hamilton
889
10 nov 2011 02:21:56
Domande correlate
1
risposte
1
risposte
4
risposte
1
risposte
1
risposte