I am trying to create a new post with tags. Not having any luck and am not able to find any explanations online of what I may be doing wrong.
methods: {
createPost: function() {
let title = this.newPostTitle;
let content = this.newPostContent;
let postCategories = this.postCategories;
let tags = this.newPostTags.split(",");
let data = {
'title': title,
'content': content,
'status': 'publish',
'categories': postCategories,
'tags': tags,
};
axios.post("/wp-json/wp/v2/posts/", data, {headers: {'X-WP-Nonce': portal.nonce}})
.then(response => {
console.log(response);
})
.catch(e => {
this.errors.push(e);
console.log(this.errors);
});
this.newPostTitle = '';
this.newPostContent = '';
this.newPostTags = '';
this.postCategories = false;
}
}
I have the method property above to get the data and post it using VueJS and Axios. What do i need to do to get the tags to post correctly?
I keep getting the following error when I attempt to post
"tags[0] is not of type integer."
I am trying to create a new post with tags. Not having any luck and am not able to find any explanations online of what I may be doing wrong.
methods: {
createPost: function() {
let title = this.newPostTitle;
let content = this.newPostContent;
let postCategories = this.postCategories;
let tags = this.newPostTags.split(",");
let data = {
'title': title,
'content': content,
'status': 'publish',
'categories': postCategories,
'tags': tags,
};
axios.post("/wp-json/wp/v2/posts/", data, {headers: {'X-WP-Nonce': portal.nonce}})
.then(response => {
console.log(response);
})
.catch(e => {
this.errors.push(e);
console.log(this.errors);
});
this.newPostTitle = '';
this.newPostContent = '';
this.newPostTags = '';
this.postCategories = false;
}
}
I have the method property above to get the data and post it using VueJS and Axios. What do i need to do to get the tags to post correctly?
I keep getting the following error when I attempt to post
Share Improve this question edited Jun 21, 2017 at 19:58 Zach Tackett asked Jun 21, 2017 at 19:15 Zach TackettZach Tackett 1931 gold badge2 silver badges6 bronze badges 1 |"tags[0] is not of type integer."
2 Answers
Reset to default 1Sounds like it's expecting tag IDs as integers and you're sending strings. split
returns an array of strings, even if those strings are numbers. Instead of using .split
try something like this
let tags = JSON.parse("[" + this.newPostTags + "]");
If you really like split then you would do this:
let tags = this.newPostTags.split(",").map(Number);
It's actually expecting a string of comma-separated concatenated tag ids (if anyone is still looking for an answer to this).
Your data would be something like the following, with your split code commented out:
let tags = this.newPostTags; // .split(",");
let data = {
'title': title,
'content': content,
'status': 'publish',
'categories': postCategories,
'tags': tags,
};
split
returns an array of strings, even if those strings are numbers. instead of split try something like this: – mrben522 Commented Jun 21, 2017 at 20:12