背景
我希望能够将 json 文件传递给 WP CLI,以迭代地创建帖子。
所以我想我可以创建一个 JSON 文件:
[
{
"post_type": "post",
"post_title": "Test",
"post_content": "[leaflet-map][leaflet-marker]",
"post_status": "publish"
},
{
"post_type": "post",
"post_title": "Number 2",
"post_content": "[leaflet-map fitbounds][leaflet-circle]",
"post_status": "publish"
}
]
并用 jq 迭代数组:
cat posts.json | jq --raw-output .[]
我希望能够迭代这些以执行类似的功能:
wp post create \
--post_type=post \
--post_title='Test Map' \
--post_content='[leaflet-map] [leaflet-marker]' \
--post_status='publish'
有没有办法可以用jq
或类似方法做到这一点?
到目前为止,我得到的最接近的是:
> for i in $(cat posts.json | jq -c .[]); do echo $i; done
但这似乎与字符串中的(有效)空格有关。输出:
{"post_type":"post","post_title":"Test","post_content":"[leaflet-map][leaflet-marker]","post_status":"publish"}
{"post_type":"post","post_title":"Number
2","post_content":"[leaflet-map
fitbounds][leaflet-circle]","post_status":"publish"}
我用这种方法是否可行,或者可以做到吗?