I need to generate a CSV report with information that is stored into a few custom fields of a custom post type. How do I list just the content of these custom fields, for all — or a subset — of these custom post types?

(I'm trying to use wp-cli to achieve this, because it's an extremely powerful and versatile tool that never failed me before. Sorry if I'm approaching it as an "X-Y problem"!)

For now all I got is wp post list --post_type=<MYCPT> --posts_per_page=1 to get me the most recent post, and then wp post meta list <ID> with the ID returned in the previous command.

This lists me all the custom fields for a single custom post. What I need, however, is the content of just a few custom fields, for all custom posts of this type.

I suppose I can write a bash script to iterate through everything and grep just the custom fields I want, but is there an easier way?

1 Answer 1


I managed to achieve what I wanted by piping wp-cli with xargs and grep, like this:

wp post list --post_type=CPTNAME --field=ID | xargs -n 1 wp post meta list --format=csv | grep -E "^[[:digit:]]*,(CUSTOMFIELD1|CUSTOMFIELD2|...|CUSTOMFIELDN)" >> file.csv

This is, however, extremely slow, taking more than 30 minutes for 10 thousand CPTs.

I think a direct SQL query would be much, much more efficient.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.