0

I'm using this code to import posts from a csv file, which works great except for the meta_input (custom fields)

while IFS=$'\t', read -r col1 col2 col3 col4 col5 col6; \
do \
 wp post create \
  --post_title="$col1" \
  --post_content="$col2" \
  --post_category="$col3" \
  --post_author=2 \
  --post_type=post \
  --post_status=publish \
  --meta_input='{"Length": $col4,"Height": $col5}' \
  ; done < ../import.csv

Error: Warning: Invalid argument supplied for foreach() in /home/*****/public_html/wp-includes/post.php on line 3791 Success: Created post 287.

Using quotes like this doesn't give an error:

--meta_input='{"Length": "$col4","Height": "$col5"}' \

But that adds the literal string '$col4' instead of the value of variable $col4.

I've tried no quotes, single quotes, double quotes in various places, but I can't figure out the correct syntax.

Line 3791 in post.php is:

        foreach ( $postarr['meta_input'] as $field => $value ) {
1

This is what ended up working for me:

meta1='{"Length":'
meta2=',"Height":"'
meta3='"}'

while IFS=$'\t', read -r col1 col2 col3 col4 col5 || [ -n "$col1" ]
do \

meta=$meta1
meta+=$col4
meta+=$meta2
meta+=$col5;
meta+=$meta3

And for meta_input line

    --meta_input=$meta \

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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