The situation is the following:

The blog as of now has 2200+ posts. In the beginning (2007/2008) I was using manual excerpts where I would put an image and a little bit of text. The manual excerpt (excerpt field populated with an image and text) is shown on archives and wherever wanted.

However recently I decided not to use the manual excerpt (excerpt field populated with an image and text). I switched to the automatic wordpress excerpt, limit the number of words, and fetch the first image in the post (if there is any) wherever I want it. Way more flexibility that way. In order to retain the uniformity of the site I use conditional tag has_excerpt () to adjust or apply new css rules to those posts who have manual excerpt. The conditional tag checks if the post uses the excerpt field (has manual excerpt).

Now I want to remove all manual excerpts (excerpt field populated with an image and text) and in the same time to label those posts (adding tag for example) in order to be able to continue to apply conditional CSS rules for those posts. For example using has_tag ().

I want to do this because those posts have slightly different images than the other posts and I have to apply slightly different CSS rules for them.

Now since there are thousands of posts, it would be insane to go all through them manually. I'm sure there is an easy way to do this via few SQl commands.

  1. Add tag (or any other label you might think is better) to all posts who have manual excerpts (excerpt field populated with an image and text).
  2. Delete the content in the excerpt field on all posts.
  • Please post your Custom Excerpt creation code...
    – brasofilo
    Jun 22, 2012 at 5:43
  • @brasofilo, I'm sorry for not being clear enough. By custom excerpt I meant the excerpt field that you can use while publishing a post. I was using that by putting an image and a little bit of text there. Latter on I decided not use that field anymore.
    – Vila
    Jun 22, 2012 at 7:42
  • @brasofilo, I've edited the question for better understanding. Basically I want to achieve the following: 1. IF post uses the Excerpt Field, ADD tag_1 to that post. 2. Delete the content in the excerpt fields on every post. I presume that this has to be done via SQL command.
    – Vila
    Jun 22, 2012 at 7:57

1 Answer 1


Some questions are easily solved searching this Stack and joining some answers together.

The following plugin will run the conversion code when activated. Adjust the $tags array, and don't forget to backup your database before proceeding.

/* Plugin Name: Remove Excerpts, Add Tags */

register_activation_hook( __FILE__, 'wpse_56133_add_tags_remove_excerpts' );    

function wpse_56133_add_tags_remove_excerpts() {
    $tags = array( 'YOUR TAG NAME A HERE', 'YOUR TAG NAME B HERE' );

    $args = array( 'post_type' => 'post', 'numberposts' => -1, 'post_status' => published );
    $posts = get_posts( $args );
    foreach ( $posts as $post )
        $po = array();
        $po = get_post( $post->ID,'ARRAY_A' );
        $po['post_excerpt'] = "";
        wp_update_post( $po );

        //set last value to false if you want to replace existing tags.
        wp_set_post_terms( intval( $post->ID ), $tags, 'post_tag', true ); 

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.