I was change my website template then i have one problem which before used template was not have thumbnail place for post for this i add all my post first image for this now on new template showing each post two pictures.

First featured image, after what i was add image showing for this problem i wanna clear first image on all my post.

What i try,

1) Manually but my site has 40000 posts thats mean not so easy to do that on manually.

2) I try to use wp-cli then i use this code

wp search-replace '/<img[^>]+\>/i' '' wp_posts --regex --allow-root

But result is

+----------+-----------------------+--------------+------+
| Table    | Column                | Replacements | Type |
+----------+-----------------------+--------------+------+
| wp_posts | post_content          | 0            | PHP  |
| wp_posts | post_title            | 0            | PHP  |
| wp_posts | post_excerpt          | 0            | PHP  |
| wp_posts | post_status           | 0            | PHP  |
| wp_posts | comment_status        | 0            | PHP  |
| wp_posts | ping_status           | 0            | PHP  |
| wp_posts | post_password         | 0            | PHP  |
| wp_posts | post_name             | 0            | PHP  |
| wp_posts | to_ping               | 0            | PHP  |
| wp_posts | pinged                | 0            | PHP  |
| wp_posts | post_content_filtered | 0            | PHP  |
| wp_posts | guid                  | 0            | PHP  |
| wp_posts | post_type             | 0            | PHP  |
| wp_posts | post_mime_type        | 0            | PHP  |
+----------+-----------------------+--------------+------+
Success: Made 0 replacements.

3) I try this code on fuctions.php

function remove_first_image ($content) {
if (!is_page() && !is_feed() && !is_feed() && !is_home()) {
$content = preg_replace("/<img[^>]+\>/i", "", $content, 1);
} return $content;
}
add_filter('the_content', 'remove_first_image');

On this solution i fix my problem but i don't want use this solution for that i'm searching any other solution with WP-CLI.

  • Is there only one image in each post or do you want to remove all images from posts? – kero Jan 21 at 14:09
  • Are you sure the <img> tags are in the database and not added dynamically via a plugin or so? – swissspidy Jan 21 at 14:15
  • each post have few image i want to delete first one @kero – ZgrKARALAR Jan 21 at 14:41
  • @swissspidy <img src="domain.com/wp-content/uploads/2018/01/image.jpg" alt="" width="660" height="250" class="aligncenter size-full wp-image-28061" /> like this on text editor i don't know about database – ZgrKARALAR Jan 21 at 14:41

According to the WP-CLI docs for the wp search-replace command (and wp db search, for that matter), you have to pass your search string without delimiters and pass any flags separately.

Instead of:

wp search-replace '/<img[^>]+\>/i' '' wp_posts --regex --allow-root

You would use something more like this:

wp search-replace '<img[^>]+\>' '' wp_posts --regex --allow-root --regex-flags='i' --regex-delimiter='/'

I just tested this on an install and I'm getting plenty of results.

Edit: WP-CLI indeed does not yet support some sort of limit parameter to only replace the first occurrence. However, I opened an issue for it so it might be possible in the future.

  • This delete all image on post not first one – ZgrKARALAR Jan 21 at 18:55
  • @ZgrKARALAR You can try to use the question mark regex flag so the search is non-greedy, i.e. …regex-flags=i? – swissspidy Jan 21 at 18:57
  • Error: The regex '/<img[^>]+\>/i?' fails. – ZgrKARALAR Jan 21 at 19:01
  • Ah right, my bad. I mixdd things up. --regex-flags='iU' should work in PHP. – swissspidy Jan 21 at 19:05
  • @swisspidy same problem as first deleting all image on post not first – ZgrKARALAR Jan 21 at 19:13

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.