Title: Programetically add tags to posts
Last modified: August 20, 2016

---

# Programetically add tags to posts

 *  [willx](https://wordpress.org/support/users/willx/)
 * (@willx)
 * [14 years ago](https://wordpress.org/support/topic/programetically-add-tags-to-posts/)
 * Ok so I’m writing a piece of php code that will automatically give (add) tags
   to posts based on logics.
 * Quick example: Any post that has “Europe” in either the post_content or post_title
   should be tagged with “Europe”.
 * I’ve gotten as far as to be able to get all the post_id’s from all posts that
   need to be tagged, I got the id of the post_tag “Europe”, and if it doesn’t exist
   I will first insert it and then get the ID, but I’ve come across a problem.
 * The problem is: when I update the tables:
    wp_terms wp_term_taxonomy wp_term_relationships
   To completely match what is needed, this meaning: – (optional: insert into wp_terms
   if not exists) and get the term_id – (optional: insert into wp_term_taxonomy 
   if not exists) and get the term_taxonomy_id – (optional: insert into wp_term_relationships
   if not exists AND increase the “count” in wp_term_taxonomy by 1)
 * However! wordpress does not tag the post, but everything in the database is correctly
   edited/inserted….
 * What am I not seeing?

Viewing 1 replies (of 1 total)

 *  Thread Starter [willx](https://wordpress.org/support/users/willx/)
 * (@willx)
 * [14 years ago](https://wordpress.org/support/topic/programetically-add-tags-to-posts/#post-2748934)
 * Also tried things like using the function
 * wp_set_object_terms
 * It does exactly the same as my own code does (makes all the connections/inserts
   everything in the database) — but it does not REALLY add the tags to the post…
   Everything is fine in the database, but when I view the posts tags in wp-admin,
   it just doesn’t show any tags at all…
 * What am I missing/overseeing?
 * _[ Please do not bump, it’s [not permitted here](http://codex.wordpress.org/Forum_Welcome#No_Bumping).]_

Viewing 1 replies (of 1 total)

The topic ‘Programetically add tags to posts’ is closed to new replies.

## Tags

 * [database](https://wordpress.org/support/topic-tag/database/)
 * [post](https://wordpress.org/support/topic-tag/post/)
 * [tag](https://wordpress.org/support/topic-tag/tag/)
 * [terms](https://wordpress.org/support/topic-tag/terms/)
 * [term_taxonomy](https://wordpress.org/support/topic-tag/term_taxonomy/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 1 reply
 * 1 participant
 * Last reply from: [willx](https://wordpress.org/support/users/willx/)
 * Last activity: [14 years ago](https://wordpress.org/support/topic/programetically-add-tags-to-posts/#post-2748934)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
