MySQL-inserted custom taxonomy term lead to 404, flush rewrites doesn’t help

Question

I import my custom terms from a desktop program using MySQL queries. But all the terms lead me to a 404 page. I tried flushing rewrites many times, it didn’t help. It only helps when I save individual terms at https://[mysite]/wp-admin/term.php?taxonomy=store-category&tag_ID=[int ID]

I create my custom taxonomy like this

$labels = some labels
$args = array(
        'labels'                     => $labels,
        'hierarchical'               => true,
        'public'                     => true,
        'show_ui'                    => true,
        'show_admin_column'          => true,
        'show_in_nav_menus'          => true,
        'show_tagcloud'              => false,
        'rewrite'                    => true
    );
    register_taxonomy( 'store-category', array( 'tdlrm_store_item' ), $args );

Then I insert my terms into my database:

//repeated for each term
//insert term into wp_terms
$wpdb->insert($wpdb->prefix.'terms', array(
    'name' => $import_group['name'],
    'slug' => $import_group['slug']
));

//get the inserted term id
$term_id = $wpdb->insert_id;

//insert the term's id from my program
$wpdb->insert($wpdb->prefix.'termmeta',array(
   'term_id' => $term_id,
   'meta_key' => 'import_id',
   'meta_value' => $import_group['id']
));

//connect to taxonomy: store-category
$wpdb->insert($wpdb->prefix.'term_taxonomy',array(
   'term_taxonomy_id' => $term_id,
   'term_id' => $term_id,
   'taxonomy' => 'store-category',
   'parent' => 0 //I have another function that inserts child items 
));

That’s it. I’ve got a term in wp_terms, it’s connected to taxonomy in wp_term_taxonomy.

Then, each product has its $import_group[‘id’], I just look for it in meta_value in wp_termmeta and insert the returned term_id as the term_taxonomy_id in wp_terms_relationships.

Looks like I’ve got everything covered, but when I go to [site root]/store-category/[term-slug]/, it leads me to a 404 page. What am I doing wrong? Is there any other place I have to insert term related data to? wp_options?

Flushing rewrite rules does not help (tried many times). Also, the number of posts shown under each of the terms is 0 (wrong), maybe this is related. Also, when I go to a term’s wp-admin page and save it, this fixes the 404 issue for this term. But I can’t do it manually, too many terms.

I’ve read this, this and similar questions but all the answers seem to be variants of ‘try to flush rewrites again’, which I did.


Oh, and I’ve just noticed. The same happens with individual posts: 404 until I refresh their term.

0
tolkodelo 3 months 0 Answers 13 views 0

Leave an answer