I have a Custom Post Type called eyeglasses
and Custom Taxonomy called models
. I also created two terms for taxonomy called M156
and M120
. Now I am trying to upload two post for test through wp_insert_post
.
This is adding the $title
to the post_title
of eyeglasses
but not adding or updating the term of the post.
function we_load_posts($title, $term)
{
wp_insert_post(array(
"post_type" => "eyeglasses",
"post_title" => $title,
"tax_input" => array(
"models" => array($term)
),
"post_status" => "publish"
));
}
we_load_posts("Montana", "M156");
we_load_posts("Havana", "M120");
can you please let me know what I am missing or doing wrong here?
1 Answer
Few points come to mind:
-
There is a typo in
"post_type" => "'eyeglasses"
(extra single quote). It should be:"post_type" => "eyeglasses"
. -
Try putting the
$term
instead ofarray( $term )
:"tax_input" => array( "models" => $term )
-
Also, is it
models
ormodel
? e.g."tax_input" => array( "model" => $term )
-
tax_input
requiresassign_terms
capability. So if the user you are running this CODE with, doesn’t have that capability, it’ll not work.In that case, the right way is:
$post_id = wp_insert_post(array( "post_type" => "eyeglasses", "post_title" => $title, "post_status" => "publish" )); wp_set_object_terms( $post_id, $term, 'model' );