Skip to content

Instantly share code, notes, and snippets.

@michael-sumner
Last active May 10, 2023 13:12
Show Gist options
  • Save michael-sumner/a6ee31ebadf8a1c4902e9527b57311fe to your computer and use it in GitHub Desktop.
Save michael-sumner/a6ee31ebadf8a1c4902e9527b57311fe to your computer and use it in GitHub Desktop.
This script prompts the user to enter a postmeta key to search for, and then retrieves the value of that postmeta key for each post on your WordPress site using the WP-CLI command wp post meta get. If the postmeta value is not "null", then the script prints the post ID to the console. This script allows you to quickly find all posts on your site…
#!/bin/bash
# Prompt the user to enter a postmeta key to search for
read -p "Enter the postmeta key to search for: " postmeta_key
# Find all posts with the specified postmeta key
post_ids=$(wp post list --format=ids)
for id in $post_ids
do
meta_value=$(wp post meta get $id $postmeta_key --format=json)
if [ -n "$meta_value" ]
then
echo $id
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment