Using sql triggers to get category into wp_em_events table
-
When users add events using Event Manager I have added a list of cities to select from as categories, which allows me to create event calendars for each city. For example, I use this shortcode to create a calendar page for events in Boston: [events_calendar full=1 category=”Boston”]. I would like to have the category name included in the wp_em_events table, so I can use it to query that table elsewhere. I don’t quite understand why it’s not included already. Especially odd, since there is a variable in the table called “event_category_id” that is null whether you include a category or not.
Anyway, I am working on a trigger in phpMyAdmin to get the category name of the event added to the wp_em_events table. It’s a bit of a puzzle to find the category name, but here is how it works:
In the wp_em_event table, each new event has a post_id.
The post_id from the wp_em_event table matches the object_id in the wp_term_relationships table.
The term_taxonomy_id from the wp_term_relationships table matches the term_id in the wp_term_taxonomy table.
The term_id in wp_term_taxonomy matches the term_id in the wp_terms table. The wp_terms also contains the variable “name,” which is the name of the category (e.g., Boston).
In other words:
wp_em_events.post_id = wp_term_relationship.object_id
wp_term_relationship.term_taxonomy_id = wp_term_taxonomy.term_id
wp_term_taxonomy.term_id = wp_terms.term_idSo, I created a new field in wp_em_events called “chapter.” If I use this sql code, I can add the category name to chapter:
UPDATE wp_em_events LEFT JOIN wp_term_relationships on (wp_em_events.post_id = wp_term_relationships.object_id) LEFT JOIN wp_term_taxonomy on (wp_term_relationships.term_taxonomy_id=wp_term_taxonomy.term_taxonomy_id) LEFT JOIN wp_terms on (wp_term_taxonomy.term_id=wp_terms.term_id) SET wp_em_events.chapter = wp_terms.name;
I want to set a trigger to insert the wp_terms.name into wp_em_events.chapter every time a new row is inserted into wp_em_events. I created the trigger with the following code:
DROP TRIGGER IF EXISTS
test`;
CREATE DEFINER=wordpressuser202
@localhost
TRIGGERtest
BEFORE INSERT ONwp_em_events
FOR EACH ROW
BEGIN
UPDATE wp_em_events
LEFT JOIN wp_term_relationships on (NEW.post_id = wp_term_relationships.object_id)
LEFT JOIN wp_term_taxonomy on (wp_term_relationships.term_taxonomy_id=wp_term_taxonomy.term_taxonomy_id)
LEFT JOIN wp_terms on (wp_term_taxonomy.term_id=wp_terms.term_id)
SET NEW.chapter = wp_terms.name;
END
`
The only difference is I changed wp_em_events. to NEW. so post_id and chapter refer to the column of the new row to be inserted. When I insert a new record, however, I get an error:#1096 – No tables used
Any thoughts on how to fix this?
- The topic ‘Using sql triggers to get category into wp_em_events table’ is closed to new replies.