Gestione dei post lato codice
Lato sviluppo, i post possono essere creati, cancellati e modificati senza accedere al backend. Ad esempio possiamo creare un post ogni volta che carichiamo una pagina. Per farlo, sempre ricordando che viene caricato functions.php, possiamo scrivere li o in un file da lui incluso il nostro codice:
//CREAZIONE :
$postarray = array(
"post_title" => $descrizione,
"post_status" => "publish",
"post_type" => "marker", // custom post type
'post_author' => 1,
);
$id_post_creato = wp_insert_post($postarray, true);
update_field("descrizione_marker", $descrizione, $id_post_creato); // una volta creato posso anche definirne eventuali campi custom ACF (vedere dopo)
//MODIFICA :
// Update post 37
$postarray = array(
'ID' => 37,
'post_title' => 'This is the post title.',
'post_content' => 'This is the updated content.',
);
// Update the post into the database
wp_update_post( $postarray );
//CANCELLAZIONE :
// imposto la query
$wpquery = new WP_Query(array(
'post_type' => "post", // oppure nome del custom post type
'posts_per_page' => -1
));
while ($wpquery->have_posts()): $wpquery->the_post();
$post_id = get_the_ID();
wp_delete_post( $post_id, true );
endwhile;
