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.


I tried deleting data from rewrite_rules in wp_options too, no use.


I changed 'rewrite' => true to 'rewrite' => false, and I don't get 404 page now, but still no idea how to fix the issue with rewrite turned on.

All irrelevant plugins are deactivated

Was it helpful?

Solution

EUREKA!

My taxonomy name is 'store-category'. When i used 'rewrite' = true or 'rewrite' = array('slug' => 'category') it was giving me a 404 error. When I changed it to 'rewrite' => array('slug' => 'group') the problem just went away!

So, the problem turned out to be a rewrite conflict. WordPress had been looking for my store-categories within categories, hence the 404 error.

No idea how 'store-category' fits in (when rewrite is just set true), but obviously it caused the same problem.

So, SOLUTION: don't use slugs that match default WP slugs; try changing your slug to something else.

Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange
scroll top