Updating Post Meta with Backbone and the REST API

I’m getting started with a project that is making use of Backbone and the WP REST API, but I”m having a little trouble.

Using the built in API JS for models and collections allows me to pretty easily update a post title or content. All I have to do is call:

this.model.set({title: 'New Title'});
this.model.save();

And everything works really well. But for post meta, there doesn’t appear to be an easy way to update an entry to the database. Does anyone know how to go about taking a post model in Backbone, and updating it’s meta data?

2 Answers
2

var parentId = 96; // the post id
var metaData = new wp.api.collections.PostMeta('', {parent: parentId});
metaData.fetch()
  .done(function(data) {
    var someKey = data.findWhere({key: 'someKey'});
    someKey.set('value', 'newValue');

    someKey.save({parent: parentId});
  });

Leave a Comment