Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new --meta_format to wp post meta commands #142

Open
jgraup opened this issue Feb 9, 2018 · 10 comments
Open

Add new --meta_format to wp post meta commands #142

jgraup opened this issue Feb 9, 2018 · 10 comments

Comments

@jgraup
Copy link

jgraup commented Feb 9, 2018

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
@schlessera
Copy link
Member

schlessera commented Feb 14, 2018

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.

@danielbachhuber
Copy link
Member

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 danielbachhuber changed the title Request: Add new --format to wp post meta commands Add new --size_format to wp post meta commands Mar 20, 2018
@schlessera schlessera changed the title Add new --size_format to wp post meta commands Add new --meta_format to wp post meta commands Mar 20, 2018
@drzraf
Copy link

drzraf commented Jul 22, 2020

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.

@Lookya23
Copy link

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

@BhargavBhandari90
Copy link
Contributor

@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"}

@danielbachhuber
Copy link
Member

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

@drzraf
Copy link

drzraf commented Aug 23, 2022

(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)

@danielbachhuber
Copy link
Member

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

@drzraf
Copy link

drzraf commented Aug 26, 2022

  • 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

@danielbachhuber
Copy link
Member

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants