Fu-Raz Fu-Raz - 4 years ago 72
MySQL Question

Adding Wordpress categories to database, changes only show after update via interface

I'm injecting Wordpress categories into my MySQL database. This is fine when it comes to categories without a parent. But when I add a child category it doesn't show in the Wordpress category list.

I believe this has something to do with Wordpress caching the query or the results for the category tree. I have tried to use this function to force Wordpress to clean the cache:

clean_term_cache( $ids, $taxonomy, $clean_taxonomy );


Found here: https://codex.wordpress.org/Function_Reference/clean_term_cache

Unfortunately it still only shows my changes after I edit and save one of the visible categories.

So to be clear: The things I do in the database work. I add the records to the right tables. The only problem is that the Wordpress admin interface doesn't show the changes. Until I use said interface to edit / save a category that IS visible in the admin interface.

Also: I use direct database injection because I'm using WooCommerce + WPML. The API's don't support creating multi-lingual categories.

Answer Source

The problem I had was due to the fact that I was trying to recursively add categories and translate them. I had to add a category to the database with one translation and add the other translations afterwards. But whenever I ran the functions to clear the cache, WPML also went to work. This messed up the whole translation.

I ended up creating a category myself and immediately deleting it after running all of my code. This seemed to work.

It hurts my programmer heart to share this, but this is how I fixed my problem. So here goes:

$temp_term = wp_insert_term('Fake category', 'product_cat', ['description' => 'Temporary category', 'slug' => 'fake-cat-temp']);
wp_delete_term($temp_term['term_id'], 'product_cat');
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download