$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>wp insert post - WP-CLI --meta-input error: Warning: Invalid argument supplied for foreach()|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

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

matteradmin12PV0评论

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 \
Post a comment

comment list (0)

  1. No comments so far