Skip to content

Instantly share code, notes, and snippets.

@trepmal
Created August 6, 2020 16:32
Show Gist options
  • Save trepmal/1391abac6766ac2baf4be30676966e47 to your computer and use it in GitHub Desktop.
Save trepmal/1391abac6766ac2baf4be30676966e47 to your computer and use it in GitHub Desktop.
#!/bin/bash
echo "post_id,post_title,image_path";
while read -r post_row
do
# --format=csv always has a header
if [[ "ID,post_title" == $post_row ]]; then
continue;
fi;
post_id=${post_row%,*}
post_title=${post_row#*,}
echo -n $post_row',';
thumbid=$(wp post meta get $post_id _thumbnail_id)
if [ ! -z $thumbid ]; then
wp post meta pluck $thumbid _wp_attachment_metadata file
else
echo '';
fi;
done < <(wp --skip-plugins --skip-themes post list --post_type=page --fields=ID,post_title --format=csv)
@trepmal
Copy link
Author

trepmal commented Aug 6, 2020

  1. get relevant posts (L32)
  2. get post's featured image (L16)
  3. if image, get image's file path else blank (L17-21)
  4. output as csv

This uses a while loop because of the post titles being fetched... spaces throw off a for loop.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment