wp_insert_post aggiungi meta_input
Nella documentazione di wp_insert_post
c'è un changelog a metà pagina che dice:
Da: WordPress 4.4.0 Un array 'meta_input' può ora essere passato a $postarr per aggiungere metadati al post.
Sto usando WordPress 4.4.2. Sto cercando di aggiungere un nuovo post eseguendo il codice come segue:
function handle_post($post)
{
wp_insert_post( array(
'post_title' => $post['title'],
'post_type' => 'werknemers',
'meta_input' => array(
array(
'key' => 'name',
'value' => $post['name']
),
array(
'key' => 'city',
'value' => $post['city']
)
)
) );
}
Il post viene aggiunto al database, ma senza i metadati.
Ho trovato questo post su Stack, ma non riesco a capire come implementare l'if statement
.
Sono anche interessato al modo di aggiungere le tassonomie (tax_input).

meta_input
è semplicemente un array monodimensionale con chiave => valore
:
'meta_input' => array(
'name' => $post['name'],
'city' => $post['city']
)
tax_input
è leggermente diverso, con la tassonomia come chiave e un array di valori:
'tax_input' => array(
'taxonomy_name' => array(
'term',
'term2',
'term3'
)
)
Nota che affinché tax_input
funzioni, l'utente attualmente loggato quando il codice viene eseguito deve avere la capacità di gestire quella tassonomia, altrimenti fallirà silenziosamente.
