wp_insert_post() o simili per custom post type

18 lug 2013, 14:09:06
Visualizzazioni: 80.5K
Voti: 8

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.

3
Commenti

Hai registrato un custom post type chiamato custom_post prima di usare questa chiamata?

Rohit Pande Rohit Pande
18 lug 2013 14:32:44

sì, è registrato

rashid rashid
18 lug 2013 14:56:08

non importa, funziona, c'era un piccolo bug nel file, questo snippet esatto è corretto. basta sostituire 'custom_post' con il post type che preferisci!

rashid rashid
18 lug 2013 15:02:55
Tutte le risposte alla domanda 5
2
20

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);
}
24 giu 2014 09:03:13
Commenti

Il parametro 'meta_input' può essere utilizzato nell'array 'wp_insert_post' per aggiungere campi meta, invece di usare 'add_post_meta' successivamente.

AncientRo AncientRo
10 mar 2019 16:14:59

Grazie per il post. @AncientRow, puoi fornire un esempio che includa meta_input?

Pegues Pegues
22 apr 2020 20:39:22
0
18

Dal Codex:

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'
));
18 lug 2013 15:21:05
0

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,
    ),
));
17 ott 2021 01:15:09
1

Ho scoperto che utilizzare isset() mi ha permesso di usare wp_insert_post() sui custom post type:

if ( !isset( $id ) ) { 
    $id = wp_insert_post( $new, true ); 
}
10 mar 2015 22:00:54
Commenti

Per formattare correttamente il codice nella tua risposta (o domanda), evidenzialo e clicca {} sopra la casella di modifica.

Gabriel Gabriel
10 mar 2015 22:38:51
0

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.

15 giu 2021 17:23:03