Skip to content

Add --update-attachment-refs flag to wp media regenerate#242

Merged
swissspidy merged 6 commits into
mainfrom
copilot/offer-flag-update-post-content
Mar 20, 2026
Merged

Add --update-attachment-refs flag to wp media regenerate#242
swissspidy merged 6 commits into
mainfrom
copilot/offer-flag-update-post-content

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Mar 20, 2026

  • Add --update-attachment-refs flag to wp media regenerate command docblock
  • Extract flag value in regenerate() and pass to process_regeneration()
  • Update process_regeneration() to capture old image URLs before regeneration
  • After regeneration, collect all changed URL pairs and issue a single batched UPDATE per attachment using nested REPLACE() calls and a combined OR WHERE clause (avoids N full-table scans)
  • Add private helper method update_post_content_for_attachment() — accepts array<string,string> of old→new URL pairs, builds nested REPLACE() SQL
  • Exclude post_type = 'revision' rows from both the SELECT (for cache invalidation) and the UPDATE to avoid corrupting revision history
  • Fetch affected post IDs before UPDATE and call clean_post_cache() per ID; also bump posts group last_changed for query cache invalidation
  • Add Behat test scenario for the new flag
  • Run linting/static analysis to verify changes (PHPCS passes; PHPStan errors are pre-existing unrelated deprecation warnings)
Original prompt

This section details on the original issue you should resolve

<issue_title>Offer flag to update post content when regenerating thumbnails</issue_title>
<issue_description>Describe the current, buggy behavior

I’m using wp-cli 2.4.0. I recently built a client a new theme and updated the large image size to fit their new theme. I then ran the command wp media regenerate --image_size=large --only-missing to regenerate new large thumbnails. Everything worked as planned. All new images were created, the old were removed, however none of the posts were updated with the new image size. They were all referencing the old images even though they no longer exists

Describe how other contributors can replicate this bug

Update large image size in settings > media
run command wp media regenerate --image_size=large --only-missing

Describe what you would expect as the correct outcome

The command would go through all images and regenerate the large image size to the new image size. It should delete the old images and update the posts to which those images were attached with the new image size.

Let us know what environment you are running this on
Dev, Staging, Production

genesis-jol (staging)* wp media regenerate 22633 --image_size=large --only-missing
Found 1 image to regenerate.
1/1 Regenerated "large" thumbnail for "Healthy Apple Crisp Recipe (Quick &#038; Easy)-5" (ID 22633).
Success: Regenerated 1 of 1 images.
```</issue_description>

## Comments on the Issue (you are @copilot in this section)

<comments>
<comment_new><author>@janw-me</author><body>
**Steps to reproduce.**

 1. Upload a (big image). Let's say it get ID 18
 2. Edit a post/page. Insert the image in large format.
 3. Go to media settings. 'large is 1024x432' by default.
    Change it to 1280x540 for example.
 4. Run `wp media regenerate 18 --image_size=large --only-missing`
    Now the image-1280x540.jpg is created.
 5. In the post the 1024x432 image is still included.
    The image of 1024x432 does still exists, which is expected.

**Solution 1.**
Replacing the new image by setting a flag.

We have the old file name. Before the image is regenerated the old size is stored in the `_wp_attachment_metadata` meta.
After that it's a basic search-replace for the post_content column for old name. > new name.

Questions.

 1. Should this be a search-replace on the post or a seperate `wp_post_update` so it's a propper revision?
 2. Which post types are to be affected? All? Only the publicly available?
 3. The same question for post status. Published or also Drafts? What about trash?
 4. This might get real slow if there are many posts and images and all images & sizes are regenerated.
 5. If the old size is replaced everywhere should the old size be removed?
    This might cause problems if images are linked outside the current WordPress like in a newsletter or on a multisite.
    I guess this could be an other flag-option.
 6. I have no idea if this might break some page builder.

**Solution 2.**
Add an option for the output as CSV.

`wp media regenerate 18 --image_size=large --format=csv`

ID,oldfile,newfile,imagesize
18,2021/10/image-1024x432.jpg,2021/10/image-1280x540.jpg,large


This way every user can loop over this output themselves and the questions for Solution 1 can be implemented freely based on every usecase.</body></comment_new>
<comment_new><author>@danielbachhuber</author><body>
I removed the `bug` label from this issue and changed the title to `Offer flag to update post content when regenerating thumbnails`

We're fixing the missing metadata bug with https://github.com/wp-cli/media-command/issues/130</body></comment_new>
</comments>


📍 Connect Copilot coding agent with Jira, Azure Boards or Linear to delegate work to Copilot in one click without leaving your project management tool.

@github-actions github-actions Bot added command:media Related to 'media' command command:media-regenerate Related to 'media regenerate' command scope:distribution Related to distribution state:unconfirmed labels Mar 20, 2026
@codecov
Copy link
Copy Markdown

codecov Bot commented Mar 20, 2026

Codecov Report

❌ Patch coverage is 95.16129% with 3 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
src/Media_Command.php 95.16% 3 Missing ⚠️

📢 Thoughts on this report? Let us know!

Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
Copilot AI changed the title [WIP] Add flag to update post content when regenerating thumbnails Add --update-attachment-refs flag to wp media regenerate Mar 20, 2026
Copilot AI requested a review from swissspidy March 20, 2026 08:17
@swissspidy swissspidy requested a review from Copilot March 20, 2026 09:05

This comment was marked as resolved.

Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
@swissspidy swissspidy marked this pull request as ready for review March 20, 2026 10:00
@swissspidy swissspidy requested a review from a team as a code owner March 20, 2026 10:00
@swissspidy swissspidy requested a review from Copilot March 20, 2026 11:58

This comment was marked as resolved.

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
@swissspidy swissspidy merged commit 1d86009 into main Mar 20, 2026
59 checks passed
@swissspidy swissspidy deleted the copilot/offer-flag-update-post-content branch March 20, 2026 21:36
@swissspidy swissspidy added this to the 2.2.7 milestone Mar 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

command:media Related to 'media' command command:media-regenerate Related to 'media regenerate' command scope:distribution Related to distribution state:unconfirmed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Offer flag to update post content when regenerating thumbnails

3 participants