$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>customization - Adding tags when creating new post|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

customization - Adding tags when creating new post

matteradmin8PV0评论

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

"tags[0] is not of type integer."

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
  • 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 Commented Jun 21, 2017 at 20:12
Add a comment  | 

2 Answers 2

Reset to default 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);

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,
};
Post a comment

comment list (0)

  1. No comments so far