InfoHeap
Tech
Navigation
  • Home
  • Tutorials
    • CSS tutorials & examples
    • CSS properties
    • Javascript cookbook
    • Linux/Unix Command Line
    • Mac
    • PHP
      • PHP functions online
      • PHP regex
    • WordPress
  • Online Tools
    • Text utilities
    • Online Lint Tools
search

Wordpress Mysql Query tutorials

  • How to delete orphan wordpress wp_postmeta rows
  • Mysql query to find all yoast meta description
  • Query to find recently modified posts in Wordpress
  • Wordpress - an approach to bulk add tags to posts
  • Wordpress - query to dump all categories
  • Wordpress - query to dump all posts with featured image
  • Wordpress - query to dump all tags
  • Wordpress - query to find all posts for a category
  • Wordpress - query to find term_taxonomy_id from category name
  • Wordpress - query to find term_taxonomy_id from tag name
  • Wordpress mysql query to get all custom keys and values for a post
  • Wordpress mysql query to get all posts with a missing custom field
  • Wordpress mysql query to get all posts with a specific custom field
  • migrate multiple mysql databases using mysqldump on Linux
 
  • Home
  • > Tutorials
  • > Wordpress
  • > Wordpress Mysql Queries

WordPress – an approach to bulk add tags to posts

By admin on Jan 8, 2016

We’ll bulk add a tag to all posts in a category as a simple use case. But this approach can be modified to handle other use cases also.

  1. First find the term_taxonomy_id for the term you want to add. Lets assume its value is 183 for the purpose of this tutorial.
  2. Now run the following query to take a backup of wp_term_relationships

    create table wp_term_relationships_bak AS select * from wp_term_relationships;
    
  3. We’ll use the following query template (assuming post_id value is 7931) to insert a tag in wp_term_relationships for a post.

    INSERT INTO wp_term_relationships (object_id, term_taxonomy_id) VALUES (7931, 183);
    
  4. To bulk update all posts matching our criteria (e.g. all posts belonging to term_taxonomy_id 84) construct a query which returns post_id and term_taxonomy_id we want to insert (183 in our case) as shown below:

    select wp_posts.ID, 183 from wp_posts join wp_term_relationships on (wp_posts.ID = wp_term_relationships.object_id)
    where wp_term_relationships.term_taxonomy_id = 84;
    
  5. Run to following query to insert the new tag for all the posts as returned by above query. Note that insert will fail if there is a duplicate.

    INSERT INTO wp_term_relationships (object_id, term_taxonomy_id) 
    select wp_posts.ID, 183 from wp_posts join wp_term_relationships on (wp_posts.ID = wp_term_relationships.object_id)
    where wp_term_relationships.term_taxonomy_id = 84;
    
  6. In addition you may have to update plugins which maintains cache based in tag name. One such plugin is Yes Another Related Posts Plugin (YARPP). You can look at this thread for updating YARPP cache.

Suggested posts:

  1. Apache – how to remove php extension from url
  2. HTML li tag
  3. PHP check if key exists in array
  4. Chrome – view javascript errors
  5. Git – how to view diff for a given commit id
  6. How to create Linux instance on Amazon AWS/EC2 Classic
  7. WordPress – query to find all posts for a category
  8. Curl – follow redirects
Share this article: share on facebook share on linkedin tweet this submit to reddit
Posted in Tutorials | Tagged Wordpress, Wordpress Mysql Queries
  • Browse content
  • Article Topics
  • Article archives
  • Contact Us
Popular Topics: Android Development | AngularJS | Apache | AWS and EC2 | Bash shell scripting | Chrome developer tools | Company results | CSS | CSS cookbook | CSS properties | CSS Pseudo Classes | CSS selectors | CSS3 | CSS3 flexbox | Devops | Git | HTML | HTML5 | Java | Javascript | Javascript cookbook | Javascript DOM | jQuery | Kubernetes | Linux | Linux/Unix Command Line | Mac | Mac Command Line | Mysql | Networking | Node.js | Online Tools | PHP | PHP cookbook | PHP Regex | Python | Python array | Python cookbook | SEO | Site Performance | SSH | Ubuntu Linux | Web Development | Webmaster | Wordpress | Wordpress customization | Wordpress How To | Wordpress Mysql Queries | InfoHeap Money

Copyright © 2025 InfoHeap.

Powered by WordPress