entity-command
entity-command copied to clipboard
Add new --meta_format to `wp post meta` commands
According to the WP-CLI 1.5.0 Announcement:
The --meta_input option of the
post createandpost updatecommands now accepts JSON-formatted arrays, so you can add or update your post and its meta in one go:
$ wp post create --post_title='Title' --post_content='Content.' --meta_input='{"key1":"value1","key2":"value2"}
Success: Created post 123.
Unfortunately, there doesn't seem to be an equivalent export option for post meta list or post meta get.
wp post meta list 123 --fields=meta_key,meta_value --format=json will create [{"meta_key":"foo", "meta_value":"bar}] and not {"foo":"bar"} as shown in the examples for post update and post create.
[--format=<format>]
Accepted values: table, csv, json, count. Default: table
[--format=<format>]
Accepted values: table, json. Default: table
[--format=<format>]
Render output in a particular format.
---
default: table
options:
– table
– csv
– ids
– json
– count
– yaml
A suggestion has been made to add a new --format=meta_input or similar.
While the argument meta_input makes sense to solve this particular issue (based on import) it doesn't really describe the export type which is <key>:<value> JSON.
--format=meta_input--format=json-key-value--format=json-key:value--format=jsonkv--format=json-kv
The technical term for a <key>:<value> array is an "associative array". So, another possible name would be --format=associative-array, or shorter --format=assoc.
Given --format=<format> is already tied to output formatting (json, csv, etc.), I think this makes more sense as a standalone argument: --meta_format=<format>.
See also wp db size --size_format=<format>
Current workaround: jq -c 'map({(.meta_key):.meta_value}) | add'
But another problem is that contrary to wp post meta get --format=json, a meta-export using wp post meta list N --format=json will not unserialize metas like an ACF flexible field a:22:{i:0;s:10:"banner.... This is unsuitable for import (which would be double-serialized at import), and hardly deserializable using jq.
Hi, i have same issue, do you find solution ?
@danielbachhuber are we looking for this command here? wp post meta list 1 --meta_format which output key:value pair as follows?
{"foo":"bar", "foo1":"bar1"}
@BhargavBhandari90 Honestly, I'm not sure how we should solve this problem, or whether it's a problem worth solving.
Exporting wp post meta as an associative array is potentially lossy because two post meta can exist with the same key.
I'd be curious to hear more about the use case of exporting post meta for use with --meta_input=<value>.
(dumping, debugging, backup'ing, importing)
Post full copy (inc. meta) across WP instance is a use-case I frequently experimented. (Some page are cluttered with tons of ACF fields where lightweight page-cloning [not export/import] can make the difference)
@drzraf Makes sense.
For lack of a better solution, I think I'd lean towards a special-case --format=jsonkv argument for wp post meta list.
What should happen when two entries exist for the same meta key?
-
I think consistency between export and import format should is the main criterium.
-
About
listvskey/value, the drawback of the second is obvious as it doesn't reproduce perfectly the WP database-schema by omitting some values. -
But key/value has so many advantages when it comes to meta:
- can extract/filter/map/... a meta by its key in the object (using
jq / whatever tool) instead of iterating = overall more friendly to developers - deduplication could be guaranteed (subsequent import/export/import/export/... would not create duplicate nor grow the overall number of meta)
- relying on multiple occurrences of a given meta_key for one post seems an edge-case in the first place
- can extract/filter/map/... a meta by its key in the object (using
Another option to throw in the mix: introduce something like --meta_list_input=<json> to handle data produced by wp post meta list --format=json.