Bulk add tag (or any sort of label) to posts who have custom excerpt

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.

Read More

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.

Related posts

Leave a Reply

1 comment

  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.

    <?php
    /* 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 ); 
        }
    }