wp_insert_post() o simili per custom post type
Ho bisogno di inserire oggetti di tipo custom post type dal codice. Non sono riuscito ad aggiungerli usando il metodo predefinito
$id = wp_insert_post(array('post_title'=>'casuale', 'post_type'=>'custom_post'));
crea invece un post normale.

Può essere fatto utilizzando il seguente codice :-
Per inserire un nuovo post per un tipo personalizzato
$post_id = wp_insert_post(array (
'post_type' => 'your_post_type',
'post_title' => $your_title,
'post_content' => $your_content,
'post_status' => 'publish',
'comment_status' => 'closed', // se preferisci
'ping_status' => 'closed', // se preferisci
));
Dopo l'inserimento del post, la funzione sopra restituirà un ID post. Ora, se vuoi inserire qualsiasi meta informazione relativa a questo post, puoi usare il seguente snippet di codice.
if ($post_id) {
// inserisci i meta del post
add_post_meta($post_id, '_your_custom_1', $custom1);
add_post_meta($post_id, '_your_custom_2', $custom2);
add_post_meta($post_id, '_your_custom_3', $custom3);
}

wp_insert_post() completerà una lista predefinita di questi ma è richiesto che l'utente fornisca il titolo e il contenuto altrimenti la scrittura nel database fallirà.
$id = wp_insert_post(array(
'post_title'=>'casuale',
'post_type'=>'custom_post',
'post_content'=>'testo dimostrativo'
));

Questo esempio ha funzionato per me utilizzando meta_input
$post_id = wp_insert_post(array (
'post_type' => 'your_post_type',
'post_title' => $your_title,
'post_content' => $your_content,
'post_status' => 'publish',
'comment_status' => 'closed',
'ping_status' => 'closed',
'meta_input' => array(
'_your_custom_1' => $custom_1,
'_your_custom_2' => $custom_2,
'_your_custom_3' => $custom_3,
),
));

Ho avuto lo stesso problema. Ho provato ogni soluzione trovata nella maggior parte dei forum. Ma la soluzione effettiva che ha funzionato per me riguardava la lunghezza del post_type. La lunghezza di post_type è limitata a 20 caratteri. Quindi, se qualcuno ha un problema simile, provi questa soluzione se nient'altro ha funzionato.
