The Fallback Image That Overwrote Every Featured Image

This blog’s deploy runs a small script of WP-CLI fixes after every code change. One of them dates from an SEO audit: posts without a featured image get a fallback image, so the structured data always has a BlogPosting.image to point at. Select every published post that has no _thumbnail_id, give it the fallback. Idempotent, harmless, done.

For four months it did exactly what everyone expected, as far as anyone could tell. Then every post got its own cover image in a single afternoon, and an hour later 21 of 28 posts were showing the fallback again.

The script hadn’t changed. What changed was that, for the first time, there was something for it to overwrite.

TL;DR: The selection was wp post list --meta-key=_thumbnail_id --meta-compare='NOT EXISTS'. WP-CLI hands those flags to WP_Query under the names it’s given, and WP_Query only knows meta_key and meta_compare — so the filter was silently dropped and the loop assigned the fallback to every published post on every deploy. As long as every post had the fallback, overwriting it with itself was invisible. The fix checks _thumbnail_id per post; a stub test with one post that already has an image now fails against the old script. The general trap: a "set the default if missing" step that actually means "always set the default" is undetectable until the first value that isn’t the default.

What Did It Look Like?

The afternoon went like this. Five new posts got cover images through the publishing pipeline, which uploads the file and sets featured_media over the REST API. They showed up correctly. Then the other 24 posts got theirs in one merge — and that merge touched both the content and the WordPress code, so two workflows started at the same moment: the publisher updating posts one by one, and the code deploy, which ends with the fix-up script.

When both finished, the featured-image IDs across the site looked like this:

id   featured_media  modified
19   127             09:48:18
42   127             09:48:22
...
148  127             09:48:40
81   189             09:48:54   ← own image
44   127             09:48:57
144  191             09:48:59   ← own image
82   192             09:49:02   ← own image
...
150  196             09:49:13   ← own image
158  127             09:40:03   ← had its own image an hour ago
Code language: CSS (css)

Media 127 is the fallback. Seven posts kept their own image; twenty-one didn’t.

How Do You Read a Pattern Like That?

By the timestamps, not by the IDs. Sorted by modification time, two things stand out:

  • Everything the publisher updated before about 09:48:50 is on the fallback. Everything from 09:48:59 onwards kept its image. In between it’s mixed — post 81, updated at 09:48:54, kept its image; post 44, updated three seconds later, lost it. That isn’t a pattern by post, category or file name. It’s the signature of two sequential processes overlapping: something else was walking the same posts one by one during those seconds, and whichever of the two reached a post last won.
  • The five posts from earlier in the afternoon were on the fallback too. Their modified time is still 09:40 — the publisher hadn’t touched them in this run, because their content hadn’t changed. Something reset their image without saving the post.

Both together rule out the publisher. It can’t reset a post it didn’t touch, and it doesn’t race itself. What walks every post one by one, writes post meta without updating the post, and was running in exactly that window? The deploy’s fix-up loop.

Why Did the Loop Touch Posts That Already Had an Image?

Because the filter that was supposed to stop it never reached the query:

wp post list --post_type=post --post_status=publish \
  --meta-key=_thumbnail_id --meta-compare='NOT EXISTS' --field=ID \
| while read -r post_id; do
    wp post meta update "$post_id" _thumbnail_id "$fallback_image_id"
  done
Code language: Bash (bash)

Look at the flag names. --post_type and --post_status use underscores; --meta-key and --meta-compare use hyphens. wp post list passes its flags through to WP_Query as query arguments, under the names it was given — and WP_Query has a meta_key and a meta_compare, not a meta-key and a meta-compare. Unknown query arguments aren’t an error. They’re ignored. What reached the database was "every published post", and the loop gave every one of them the fallback.

Every deploy since July had done this to all posts. Every deploy since July had changed nothing, because each post’s _thumbnail_id was already 127 and wp post meta update with the same value is a no-op. The bug was in production for four months with no observable effect, which is exactly what made it survive four months.

Why Did Nothing Catch It Earlier?

Three reasons, and none of them is unusual.

The test agreed with the bug. The script has a stub test: a fake wp binary logs its calls, and the test asserts on the log. The stub answered the exact post list … --meta-compare=NOT EXISTS … command with two IDs and the test checked both got the fallback. It tested that the script does what it says, against a stub that believes what it says. Nothing in it could notice that the real WP-CLI wouldn’t honour the filter.

The state couldn’t show it. A fix-up step that writes a default is only distinguishable from one that writes the default where it’s missing when some value isn’t the default. Until that afternoon, none was.

The step looked too simple to verify. It’s three lines. The kind of thing you read once, nod at, and never think about again — which is fine for code whose output you’d notice, and the opposite of fine for code whose output looks the same whether it’s right or wrong.

What Does the Fix Look Like?

Stop relying on the query to filter and check the condition per post:

wp post list --post_type=post --post_status=publish --field=ID | while read -r post_id; do
  if [ -z "$(wp post meta get "$post_id" _thumbnail_id 2>/dev/null || true)" ]; then
    wp post meta update "$post_id" _thumbnail_id "$fallback_image_id"
  fi
done
Code language: Bash (bash)

It’s slower — one extra WP-CLI call per post — and on 28 posts nobody will ever notice. It also doesn’t depend on getting a flag name right, which is the property that matters here. (--meta_key with an underscore would very likely have worked. But "very likely" is how the original line got written.)

The test changed in the way that matters: the stub now has a third post that already has an image, and the test fails if that post’s _thumbnail_id gets written:

*"post meta get 7 _thumbnail_id") echo 99 ;;   # has its own image
...
if grep -qF -- "post meta update 7 _thumbnail_id" "$WP_STUB_LOG"; then
  echo "FEHLER: eigenes Beitragsbild von Post 7 überschrieben" >&2
  fail=1
fi
Code language: Bash (bash)

Run against the old script (with the stub answering its query with all three posts, as the real WP-CLI did), it fails. Run against the new one, it passes. A test that can’t fail against the bug it’s meant to prevent isn’t a test of that bug.

Recovery was the boring part. Deploy the fixed script first — it left the seven correct images alone, which was the first live confirmation that the fix worked — and only then re-run the publisher with --force to set featured_media again. In that order, because the other order would have raced the old loop one more time. The publisher keeps the media ID per file, so nothing was uploaded twice.

Was There Anything Else Hiding Behind It?

One more thing, found while checking the result. The structured data on the five early posts still pointed at the fallback image, even after the featured image was right on the page. The SEO plugin generating the JSON-LD caches it in post meta for a day and clears that cache on save_post — but the REST API sets featured_media after save_post has run. The cache was rebuilt from the old image in between. The fix is a hook that clears the plugin’s cache whenever _thumbnail_id itself changes, plus a cache flush on every deploy for the case where publish and deploy run side by side.

It’s the same shape of problem in a smaller size: an invalidation keyed on the wrong event, invisible until the value it guards actually changes.

What’s the Generalizable Lesson Here?

  • WP-CLI passes post list flags to WP_Query under the names you type. WP_Query uses underscores (meta_key, meta_compare, meta_value), and unknown arguments are silently ignored. A hyphenated query flag doesn’t fail — it just stops filtering.
  • A "fill in the default where missing" step that actually writes everywhere is undetectable while every value is the default. Test it with one item that is not the default and assert it’s left alone.
  • A stub that answers the exact command your script sends will agree with any mistake in that command. Make the stub behave like the real tool on the input that matters, or test the property ("items with a value are untouched") rather than the call.
  • When damage follows modification time — all before, none after, a mixed band in between — look for a second process walking the same items at the same time. Sort by modification time before you sort by anything else.
  • Damage on items that weren’t saved points away from the code that saves them. Five untouched posts with the wrong image ruled out the publisher in one step.
  • Recover in an order that can’t re-trigger the fault: fix the step that broke it, confirm the fix live, then restore. It’s the same discipline as verifying what a write actually stored instead of trusting the success message.