0

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

6
  • Why didn't you use wp_insert_term() and add_term_meta()?
    – Sally CJ
    Commented Oct 31, 2020 at 14:59
  • because I batch import data from xml files that come in http requests, and it was more convinient for me to do it this way in this case. one of the reasons is that it's a function file inside a plugin, and I need to manually include files like wp-includes/taxonomy.php in it for wp_insert_term() to work. $wpdb was already used in the file, so it was more convinient to use it for all the tasks. Maybe it was something else, too Anyway, it doesn't matter. I've found out it applies even to the 'store-category terms I manually create in wp-admin. They give me a 404 page too
    – Artem
    Commented Oct 31, 2020 at 15:17
  • Well, if the import code/function is in a WordPress site, then those two functions I mentioned would always be ready in an active plugin. So why do you need to manually load the core WordPress files; how the function file is being loaded? And using those two functions might not going to fix the permalinks issue, but your code will be much simpler.
    – Sally CJ
    Commented Oct 31, 2020 at 15:29
  • Sally, thank you for your question. I am well aware of the two functions and use them where needed, but not in this case. It has to do with my plugin's file structure and the task.
    – Artem
    Commented Oct 31, 2020 at 15:43
  • One thing is MySQL transactions.
    – Artem
    Commented Oct 31, 2020 at 15:50

1 Answer 1

0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.