0

I am trying to find a method to delete a certain tag, that is used in my theme to display differently "featured" and "dont-miss" posts, from multiple posts and I was not able to find a solution. Or the only solution that I found is quick-edit each post manually.

I know that adding bulk tags to posts is supported directly within Wordpress, but is there also the possibility to bulk remove ta tag? Do you have a semi-automatic alternative?

  • 3
    Do you want to remove the tags from the dashboard completely? If you go to Posts > Tags and then just delete those tags, they will be removed from any posts that have them since the tags no longer exist. Also, maybe these forum topics will help: wordpress.org/support/topic/mass-remove-tags and wordpress.org/support/topic/mass-delete-tags – RachieVee Feb 20 '14 at 13:52
  • No, I don't want to remove tags completely from all posts, I only want to remove them from selected specific posts. – Drake Feb 20 '14 at 15:49
  • How are you picking up the posts from which you need to remove a particular tag? – Sudeep K Rana Feb 25 '14 at 10:58
  • You could try it the reverse way. From Tags page, delete those tags and create them again. This will remove their relation with posts. And then add bulk tags to posts. – Shazzad Feb 25 '14 at 16:01
2
+50

Since no direct function is there to achieve the required so I have made the code.

Code is self explanatory and you can tweak it the way you like.

<?php
                //Assuming a post ID to reset tags.
            $postid = 172;
                //Assuming tag2 is supposed to be removed
            $remove_tag = 'tag2';
                //Collecting all the tags of post
            $total_tags = get_the_tags($postid);
                //Recreating an array without the $remove_tag
            foreach($total_tags as $tag){
                if($tag->name != $remove_tag){
                    $updated_tags[] = $tag->name;
                }
            }
            //Setting tags with $updated_tags array.
            wp_set_post_terms( $postid, $updated_tags, 'post_tag', false);
        ?>

If you are working on multiple posts then you can run a foreach loop and by passing postid in each loop.

EDIT Code updated to include many posts (post id's)

<?php
        //Assuming a post ID to reset tags.
    $posts_to_remove_tag_from = array(172,168);
        //Assuming tag2 is supposed to be removed
    $remove_tag = 'tag2';
        //Collecting all the tags of post
    foreach($posts_to_remove_tag_from as $postid){
        $total_tags = get_the_tags($postid);
            //Recreating an array to without the $remove_tag
        foreach($total_tags as $tag){
            if($tag->name != $remove_tag){
                $updated_tags[] = $tag->name;
            }
        }
        //Setting tags with $updated_tags array.
        wp_set_post_terms( $postid, $updated_tags, 'post_tag', false);
            //flushing $updated_tags array, and make it ready for next post in the loop.
        $updated_tags = [];
    }
?>
  • Thank you, but it is not working. I copied the code in a empty file and for sure it misses some includes, because get_the_tags function is not recognized, but I cannot detect which of them. Can you help me? – Drake Feb 26 '14 at 15:50
  • Where did you paste this code, in which file? Also get_the_tags() is a wordpress core function. Codex here : codex.wordpress.org/Function_Reference/get_the_tags . Right now its too late here. Will reply tomm. – Sudeep K Rana Feb 26 '14 at 16:34
  • I placed the code in an empty file I know that this was the main problem, but I didn't know which file is more suitable to contain this code. Should I need to create a dedicated function in functions.php of my theme and then call in the index? Or should I maybe write a very simple plugin with a button to trigger that function? – Drake Feb 26 '14 at 16:57
  • You can paste this code even at index.php and once all of the tags are reset you can remove the code. Just include the post id's into $posts_to_remove_tags_from array. Or you can make a function of it, paste it at functions.php and call it from index.php – Sudeep K Rana Feb 27 '14 at 2:27
  • Ok it works! It's a bit time consuming but maybe I'll try to write a plugin starting from this code with some helper that allow an easier management of tags. – Drake Feb 27 '14 at 13:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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