Not Able to Insert Taxonomy Term Using wp_insert_post()

 Clash Royale CLAN TAG#URR8PPP
Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;
up vote
3
down vote
favorite
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?
custom-post-types custom-taxonomy wp-insert-post wp-insert-term
add a comment |Â
up vote
3
down vote
favorite
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?
custom-post-types custom-taxonomy wp-insert-post wp-insert-term
add a comment |Â
up vote
3
down vote
favorite
up vote
3
down vote
favorite
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?
custom-post-types custom-taxonomy wp-insert-post wp-insert-term
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?
custom-post-types custom-taxonomy wp-insert-post wp-insert-term
custom-post-types custom-taxonomy wp-insert-post wp-insert-term
edited Sep 5 at 6:10
asked Sep 5 at 4:27


Behseini
2731315
2731315
add a comment |Â
add a comment |Â
 1 Answer
 1
 
active
oldest
votes
up vote
4
down vote
accepted
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 - $terminstead of- array( $term ):- "tax_input" => array(
 "models" => $term
 )
- Also, is it - modelsor- model? e.g.- "tax_input" => array(
 "model" => $term
 )
- tax_inputrequires- assign_termscapability. 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' );
 
 
 
 
 
 
 Thanks for reply Nazaria, on- (1)- Thanks I fixed the typeo, on- (2)- I removed the- $termout of array on- (3 )-- modelsis the name that I use to register the taxonomy. I fixed 1 and 2 as you said but still not able to add the terms with this code. On- (4)I am not sure how to get the- $post_idas I am running this code in a PHP file out of the theme directory by including- wp-load.phpto the file so not sure how this is gonna work! I just need this script for one time only upload
 â Behseini
 Sep 5 at 6:09
 
 
 
 
 
 
 3
 
 
 
 
 The number 4 actually worked! Thanks a lots
 â Behseini
 Sep 5 at 6:15
 
 
 
 
 
 
 
 
 
 Thanks but can u please give me more hint on this ` you need to insert autho`
 â Behseini
 Sep 5 at 6:16
 
 
 
 
 
 
 
 
 
 Check documentation,- wp_insert_postargument array needs- post_author. By default, WordPress takes the currently logged in user ID, however, depending on how you are running the external script, there may not be a logged in author. So, it's better if you provide an author id, like:- "post_author" => 1
 â Nazaria
 Sep 5 at 6:19
 
 
 
 
 
 
 
 
 
 So if you don't provide an author ID and no author is logged in, WP will insert- 0as author.
 â Nazaria
 Sep 5 at 6:22
 
 
 
add a comment |Â
 1 Answer
 1
 
active
oldest
votes
 1 Answer
 1
 
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
4
down vote
accepted
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 - $terminstead of- array( $term ):- "tax_input" => array(
 "models" => $term
 )
- Also, is it - modelsor- model? e.g.- "tax_input" => array(
 "model" => $term
 )
- tax_inputrequires- assign_termscapability. 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' );
 
 
 
 
 
 
 Thanks for reply Nazaria, on- (1)- Thanks I fixed the typeo, on- (2)- I removed the- $termout of array on- (3 )-- modelsis the name that I use to register the taxonomy. I fixed 1 and 2 as you said but still not able to add the terms with this code. On- (4)I am not sure how to get the- $post_idas I am running this code in a PHP file out of the theme directory by including- wp-load.phpto the file so not sure how this is gonna work! I just need this script for one time only upload
 â Behseini
 Sep 5 at 6:09
 
 
 
 
 
 
 3
 
 
 
 
 The number 4 actually worked! Thanks a lots
 â Behseini
 Sep 5 at 6:15
 
 
 
 
 
 
 
 
 
 Thanks but can u please give me more hint on this ` you need to insert autho`
 â Behseini
 Sep 5 at 6:16
 
 
 
 
 
 
 
 
 
 Check documentation,- wp_insert_postargument array needs- post_author. By default, WordPress takes the currently logged in user ID, however, depending on how you are running the external script, there may not be a logged in author. So, it's better if you provide an author id, like:- "post_author" => 1
 â Nazaria
 Sep 5 at 6:19
 
 
 
 
 
 
 
 
 
 So if you don't provide an author ID and no author is logged in, WP will insert- 0as author.
 â Nazaria
 Sep 5 at 6:22
 
 
 
add a comment |Â
up vote
4
down vote
accepted
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 - $terminstead of- array( $term ):- "tax_input" => array(
 "models" => $term
 )
- Also, is it - modelsor- model? e.g.- "tax_input" => array(
 "model" => $term
 )
- tax_inputrequires- assign_termscapability. 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' );
 
 
 
 
 
 
 Thanks for reply Nazaria, on- (1)- Thanks I fixed the typeo, on- (2)- I removed the- $termout of array on- (3 )-- modelsis the name that I use to register the taxonomy. I fixed 1 and 2 as you said but still not able to add the terms with this code. On- (4)I am not sure how to get the- $post_idas I am running this code in a PHP file out of the theme directory by including- wp-load.phpto the file so not sure how this is gonna work! I just need this script for one time only upload
 â Behseini
 Sep 5 at 6:09
 
 
 
 
 
 
 3
 
 
 
 
 The number 4 actually worked! Thanks a lots
 â Behseini
 Sep 5 at 6:15
 
 
 
 
 
 
 
 
 
 Thanks but can u please give me more hint on this ` you need to insert autho`
 â Behseini
 Sep 5 at 6:16
 
 
 
 
 
 
 
 
 
 Check documentation,- wp_insert_postargument array needs- post_author. By default, WordPress takes the currently logged in user ID, however, depending on how you are running the external script, there may not be a logged in author. So, it's better if you provide an author id, like:- "post_author" => 1
 â Nazaria
 Sep 5 at 6:19
 
 
 
 
 
 
 
 
 
 So if you don't provide an author ID and no author is logged in, WP will insert- 0as author.
 â Nazaria
 Sep 5 at 6:22
 
 
 
add a comment |Â
up vote
4
down vote
accepted
up vote
4
down vote
accepted
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 - $terminstead of- array( $term ):- "tax_input" => array(
 "models" => $term
 )
- Also, is it - modelsor- model? e.g.- "tax_input" => array(
 "model" => $term
 )
- tax_inputrequires- assign_termscapability. 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' );
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 - $terminstead of- array( $term ):- "tax_input" => array(
 "models" => $term
 )
- Also, is it - modelsor- model? e.g.- "tax_input" => array(
 "model" => $term
 )
- tax_inputrequires- assign_termscapability. 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' );
answered Sep 5 at 5:23


Nazaria
306111
306111
 
 
 
 
 
 
 Thanks for reply Nazaria, on- (1)- Thanks I fixed the typeo, on- (2)- I removed the- $termout of array on- (3 )-- modelsis the name that I use to register the taxonomy. I fixed 1 and 2 as you said but still not able to add the terms with this code. On- (4)I am not sure how to get the- $post_idas I am running this code in a PHP file out of the theme directory by including- wp-load.phpto the file so not sure how this is gonna work! I just need this script for one time only upload
 â Behseini
 Sep 5 at 6:09
 
 
 
 
 
 
 3
 
 
 
 
 The number 4 actually worked! Thanks a lots
 â Behseini
 Sep 5 at 6:15
 
 
 
 
 
 
 
 
 
 Thanks but can u please give me more hint on this ` you need to insert autho`
 â Behseini
 Sep 5 at 6:16
 
 
 
 
 
 
 
 
 
 Check documentation,- wp_insert_postargument array needs- post_author. By default, WordPress takes the currently logged in user ID, however, depending on how you are running the external script, there may not be a logged in author. So, it's better if you provide an author id, like:- "post_author" => 1
 â Nazaria
 Sep 5 at 6:19
 
 
 
 
 
 
 
 
 
 So if you don't provide an author ID and no author is logged in, WP will insert- 0as author.
 â Nazaria
 Sep 5 at 6:22
 
 
 
add a comment |Â
 
 
 
 
 
 
 Thanks for reply Nazaria, on- (1)- Thanks I fixed the typeo, on- (2)- I removed the- $termout of array on- (3 )-- modelsis the name that I use to register the taxonomy. I fixed 1 and 2 as you said but still not able to add the terms with this code. On- (4)I am not sure how to get the- $post_idas I am running this code in a PHP file out of the theme directory by including- wp-load.phpto the file so not sure how this is gonna work! I just need this script for one time only upload
 â Behseini
 Sep 5 at 6:09
 
 
 
 
 
 
 3
 
 
 
 
 The number 4 actually worked! Thanks a lots
 â Behseini
 Sep 5 at 6:15
 
 
 
 
 
 
 
 
 
 Thanks but can u please give me more hint on this ` you need to insert autho`
 â Behseini
 Sep 5 at 6:16
 
 
 
 
 
 
 
 
 
 Check documentation,- wp_insert_postargument array needs- post_author. By default, WordPress takes the currently logged in user ID, however, depending on how you are running the external script, there may not be a logged in author. So, it's better if you provide an author id, like:- "post_author" => 1
 â Nazaria
 Sep 5 at 6:19
 
 
 
 
 
 
 
 
 
 So if you don't provide an author ID and no author is logged in, WP will insert- 0as author.
 â Nazaria
 Sep 5 at 6:22
 
 
 
Thanks for reply Nazaria, on
(1) - Thanks I fixed the typeo, on (2) - I removed the $term out of array on (3 ) - models is the name that I use to register the taxonomy. I fixed 1 and 2 as you said but still not able to add the terms with this code. On (4) I am not sure how to get the $post_id as I am running this code in a PHP file out of the theme directory by including wp-load.php to the file so not sure how this is gonna work! I just need this script for one time only uploadâ Behseini
Sep 5 at 6:09
Thanks for reply Nazaria, on
(1) - Thanks I fixed the typeo, on (2) - I removed the $term out of array on (3 ) - models is the name that I use to register the taxonomy. I fixed 1 and 2 as you said but still not able to add the terms with this code. On (4) I am not sure how to get the $post_id as I am running this code in a PHP file out of the theme directory by including wp-load.php to the file so not sure how this is gonna work! I just need this script for one time only uploadâ Behseini
Sep 5 at 6:09
3
3
The number 4 actually worked! Thanks a lots
â Behseini
Sep 5 at 6:15
The number 4 actually worked! Thanks a lots
â Behseini
Sep 5 at 6:15
Thanks but can u please give me more hint on this ` you need to insert autho`
â Behseini
Sep 5 at 6:16
Thanks but can u please give me more hint on this ` you need to insert autho`
â Behseini
Sep 5 at 6:16
Check documentation,
wp_insert_post argument array needs post_author. By default, WordPress takes the currently logged in user ID, however, depending on how you are running the external script, there may not be a logged in author. So, it's better if you provide an author id, like: "post_author" => 1â Nazaria
Sep 5 at 6:19
Check documentation,
wp_insert_post argument array needs post_author. By default, WordPress takes the currently logged in user ID, however, depending on how you are running the external script, there may not be a logged in author. So, it's better if you provide an author id, like: "post_author" => 1â Nazaria
Sep 5 at 6:19
So if you don't provide an author ID and no author is logged in, WP will insert
0 as author.â Nazaria
Sep 5 at 6:22
So if you don't provide an author ID and no author is logged in, WP will insert
0 as author.â Nazaria
Sep 5 at 6:22
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fwordpress.stackexchange.com%2fquestions%2f313363%2fnot-able-to-insert-taxonomy-term-using-wp-insert-post%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password