0

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."

  • Sounds like it's expecting a tag ID and you're sending a string. split returns an array of strings, even if those strings are numbers. instead of split try something like this: – mrben522 Jun 21 '17 at 20:12
1

Sounds 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);
0

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,
};

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.