entity-command icon indicating copy to clipboard operation
entity-command copied to clipboard

Add new --meta_format to `wp post meta` commands

Open jgraup opened this issue 7 years ago • 10 comments

According to the WP-CLI 1.5.0 Announcement:

The --meta_input option of the post create and post update commands 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.


post meta list

[--format=<format>]
Accepted values: table, csv, json, count. Default: table

post meta get

[--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

jgraup avatar Feb 09 '18 12:02 jgraup

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.

schlessera avatar Feb 14 '18 14:02 schlessera

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>

danielbachhuber avatar Mar 20 '18 12:03 danielbachhuber

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.

drzraf avatar Jul 22 '20 15:07 drzraf

Hi, i have same issue, do you find solution ?

Lookya23 avatar Jul 11 '22 04:07 Lookya23

@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 avatar Aug 21 '22 13:08 BhargavBhandari90

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

danielbachhuber avatar Aug 22 '22 19:08 danielbachhuber

(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 avatar Aug 23 '22 21:08 drzraf

@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?

danielbachhuber avatar Aug 26 '22 18:08 danielbachhuber

  • I think consistency between export and import format should is the main criterium.

  • About list vs key/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

drzraf avatar Aug 26 '22 19:08 drzraf

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.

danielbachhuber avatar Aug 26 '22 19:08 danielbachhuber