New command proposal: `wp json [import|export]`
Feature Request
- [x] Yes, I reviewed the contribution guidelines.
Describe your use case and the problem you are facing
I'd like a more flexible way to export and import data between WordPress installations. The REST API is already pretty good as a foundational building block for this task, and now what is missing is a way to do this securely / on a schedule / flexibly based on a custom query.
Describe the solution you'd like
WP-CLI + SSH would be perfect to fill in the rest of the missing pieces. Here are a couple of example commands:
wp json export posts 'ids=1,2,3&otherparams' --meta=metavalue1,metavalue2- prepare and output a JSON document based on the above parameters.wp json import --match=post_name- import the JSON document from the previous command, using post slugs to match existing posts.ssh -C servername wp json export posts 'ids=1,2,3&otherparams' --meta=metavalue1,metavalue2 | ssh -C servername2 wp json import --match=post_name- combine the previous 2 commands into one.
Calling this new command wp json is pretty logical to me since the default API path is wp-json. This command could also incorporate extra API-related subcommands such as wp json call.
I have seen https://github.com/wp-cli/restful but this proposal is a bit different given the focus on importing and exporting. I will probably be putting together a proof-of-concept sometime this week and I will post it here. At the moment I'm not sure whether it would be easier to start from scratch or try to adapt wp rest to do what I have in mind, any input is welcome.
daniel 15 minutes ago I think @dannyb was working on this for a while... Maybe it was https://github.com/dannyb195/WPCLI-Migrate-Script There are some underlying conceptual problems you'll eventually run into. Related prior art includes https://versionpress.com/, https://deliciousbrains.com/wp-migrate-db-pro/doc/mergebot/, and https://shop.crowdfavorite.com/ramp/
jnylen 12 minutes ago Yes, there are conceptual problems with "sync everything"... I'm not looking for that, more for a fine-grained sync that is under my control as the developer who knows how the site works and what needs to be synced and why A bit different, no?
daniel 11 minutes ago You'll still run into problems.
jnylen 11 minutes ago Such as?
daniel 11 minutes ago One big one is ids. If [gallery ids=1,2,3], is in the post content, what do you do? Or, if your post includes _thumbnail_id, what do you do?
jnylen 11 minutes ago Ah, yes, that's true Fortunately not something I care about too much for my initial use case :slightly_smiling_face:
daniel 10 minutes ago You can probably get to something that's ~80% working and then you'll run into these deep problems.
These would indeed be difficult problems to address. I am going to put together a proof of concept anyway to solve my current problem and we will see where it goes :)
Interested in seeing your proof of concept, and please feel free to ping me when you want to discuss implementation details.
However, I too think that this is not something you can easily solve given the brittle nature of the WordPress data model.
Hi @schlessera, yes, the idea is this would be a fairly simple tool for power users. Not a full synchronization solution but more like a building block for those who already know exactly what they want to copy and how.
I will probably be putting together a proof-of-concept sometime this week
Well, at least I said probably this time :wink:
We've solved our current task another way but I have wanted this several times before. I will drop an update here when I have one, or find you on a Slack somewhere to discuss.
At the moment I'm not sure whether it would be easier to start from scratch or try to adapt
wp restto do what I have in mind
Any thoughts on this are welcome for the next time I look at this, or for anyone else who might be interested.