I've been using this code:

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

for a few years now on my site that has almost 20,000 posts with images inserted at the top. I have a plugin that uses the first image as a featured image as well. sometimes I need to still insert a first image into content so it doesn't remove another image I have posted in there.

I would like to have the first image in the content removed completely in posts. so that in the future I don't have to insert a featured image into the content every time, just so other images show up when I have more than one image. So far I haven't been able to find anything on this except having to go into 20,000 posts and remove the first image.

Any ideas?

  • I don't think you made the relationship between the plugin that uses the first image as a featured image and what you are trying to do. Why exactly does your code no longer do what you need it to do? Are you trying to stop using the plugin and set featured images correctly using the featured image meta box?
    – totels
    Sep 7, 2015 at 14:47
  • essentially I want to use all my posts using the featured image method. I don't want to use this function to hide the first image, only to have to insert the featured image into the posts still when I'm using more than 1 image. so trying to find a way to get the first images removed, without just hiding them using this function. Sep 7, 2015 at 14:59
  • i'm trying to do this manually (for some reason) and in the database file, it lists the image twice. I'm trying to figure out which one I should remove. is one the attachment/featured image and the other is in the content? Sep 8, 2015 at 13:47
  • Post content is in wp_posts.post_content. SELECT ID, post_content FROM wp_posts LIMIT 1;
    – totels
    Sep 8, 2015 at 21:17
  • Why not just .post-content img:first-child{display:none;} //.post-content being your content wrapper? Or, you want to remove it entirely?
    – Abhik
    Aug 17, 2020 at 11:02

3 Answers 3


I don't know of any plugin specifically for this, there's nothing in WP that will do this but it's not too difficult to implement with a little php, even 20k posts shouldn't be too extreme. Depending on your server settings you may need to do some workarounds to make sure the connection stays alive but the basic idea would be to loop through all posts, check that it's a proper post (not a page, revision, custom post_type, etc.) and then run a string replace on the content very much like the code you already have.

This is untested, just as an example:

$query = new WP_Query( array(
  'post_type' => 'post',
  'post_status' => 'publish'
) );

foreach ( $query->posts as $edit_post ) {
  wp_update_post( array(
    'ID' => $edit_post->ID,
    'post_content' => preg_replace( "/<img[^>]+\>/i", "", $edit_post->post_content, 1 )

You'd probably want to put that in a plugin of it's own with some admin page code to run it someplace safe, hopefully you get the idea.

WP-CLI has the ability to do bulk edits with search-replace of DB strings and could probably be used to do something similar.

$ wp search-replace '/<img[^>]+\>/i' '' wp_posts --regex 
  • thanks..not sure how to do this. can this be put in functions.php and have it do it there? Sep 8, 2015 at 19:22
  • I would not put this in functions.php, it would likely take down a live site, it would best be placed in a custom plugin and accessed by you and you alone. You could put it in a custom page template and set the page to be password protected, but depending on your server settings it's unlikely to work without knowing a handful of other issues.
    – totels
    Sep 8, 2015 at 21:15
  • hmm don't know how to do a plugin, i'm doing this all on localhost but think this stuff is out of my league. i was trying to look through the database at the wp_posts but saw 3 ids or whatever of the same image? i posted an example of the database code at: stackoverflow.com/questions/32460133/… Sep 8, 2015 at 21:42

You could probably do this on the front end using something like this

$('img ')[0].remove()

If you only want to hide it, you can do it in CSS.

.your-post-content-class img:first-of-type { display:none; }

If you want to really remove if from post_content, you can get the first image tag in your post_content then update it.

function removeFirstImgTag($post_id) {
  $post = get_post($post_id);
  $content = $post->post_content;
  preg_match('<img([\w\W]+?)/>', $content, $matches);
  $content = str_replace($matches[0][0], '', $content);

    'ID'            => $post_id,
    'post_content'  => $content,

You get the post_content without apply_filters('the_content'), then you get the first img tag with regex, then you update it.

I haven't test this yet, but I think, that's works :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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