wp insert post - WP-CLI --meta-input error: Warning: Invalid argument supplied for foreach()

admin2025-06-02  4

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

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 ) {
Share Improve this question edited Feb 24, 2019 at 12:44 dean2020 asked Feb 24, 2019 at 12:27 dean2020dean2020 2512 silver badges6 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 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 \
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748870582a314407.html

最新回复(0)