最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Cannot delete or edit a single term in a custom taxonomy, taxonomy name is wrong?

programmeradmin0浏览0评论

I have created a custom taxonomy with the basic code below. I created a term called 'video' some time ago to test it. I created a few other terms today then noticed that I cannot delete or edit that first created term 'video'. It is grayed out and offers only the view link, which is a broken link. Oddly, using the correct permalink structure, the page for the 'video' term does display as expected.

I can create and edit other terms without any errors. There's also no errors anywhere else in the WP admin pages.

UPDATE:

After troubleshooting, I believe the issue is because I initially created the custom taxonomy with the name Purpose, but then changed it to purpose. Calling $object->taxonomy returns Purpose, while all the others listed in the admin panel return purpose.

How can I change this term's taxonomy from Purpose to purpose?

Here is the function that creates the taxonomy:

function post_purpose_taxonomy()
{
    $args = array (
        'labels' => array('name' => 'Purpose Tags', 'singular_name' => 'Purpose Tag'),
        'hierarchical' => false,
        'public' => true,
    );
    register_taxonomy('purpose', [ 'post', 'dvposts' ], $args);
}
add_action('init', 'post_purpose_taxonomy');

You can see that I register the taxonomy to both posts and the custom post type 'dvposts'.

Here is a snapshot:

I have created a custom taxonomy with the basic code below. I created a term called 'video' some time ago to test it. I created a few other terms today then noticed that I cannot delete or edit that first created term 'video'. It is grayed out and offers only the view link, which is a broken link. Oddly, using the correct permalink structure, the page for the 'video' term does display as expected.

I can create and edit other terms without any errors. There's also no errors anywhere else in the WP admin pages.

UPDATE:

After troubleshooting, I believe the issue is because I initially created the custom taxonomy with the name Purpose, but then changed it to purpose. Calling $object->taxonomy returns Purpose, while all the others listed in the admin panel return purpose.

How can I change this term's taxonomy from Purpose to purpose?

Here is the function that creates the taxonomy:

function post_purpose_taxonomy()
{
    $args = array (
        'labels' => array('name' => 'Purpose Tags', 'singular_name' => 'Purpose Tag'),
        'hierarchical' => false,
        'public' => true,
    );
    register_taxonomy('purpose', [ 'post', 'dvposts' ], $args);
}
add_action('init', 'post_purpose_taxonomy');

You can see that I register the taxonomy to both posts and the custom post type 'dvposts'.

Here is a snapshot:

Share Improve this question edited Aug 12, 2020 at 21:35 user38365 asked Jul 3, 2020 at 22:14 user38365user38365 2841 gold badge4 silver badges18 bronze badges 10
  • This is unusual, but, I notice your register_taxonomy call is very small, normally there are a lot more options declared. I also notice that you make an extra call to register_taxonomy_for_object_type rather than just passing [ 'post', 'dvposts' ] as the second parameter. Have you disabled all plugins and confirmed that none of them cause this? Or that this still happens in a blank clean slate WP install with the default theme and that code? – Tom J Nowell Commented Jul 3, 2020 at 23:10
  • @TomJNowell This is a clean install and a custom theme from scratch. I'll try passing [ 'post', 'dvposts' ] in register_taxonomy instead and see what happens. – user38365 Commented Jul 4, 2020 at 0:31
  • @TomJNowell Tried register_taxonomy('purpose', [ 'post', 'dvposts' ], $args); and removed register_taxonomy_for_object_type. That did not work. Also just tried passing 'capabilities' => array ('manage_terms','edit_terms','delete_terms','assign_terms') and no luck. – user38365 Commented Jul 4, 2020 at 0:40
  • Activating the 2020 WP theme removes the custom taxonomy. Are you suggesting registering the custom taxonomy in the 2020 WP theme for testing? – user38365 Commented Jul 4, 2020 at 0:46
  • 1 Just keep iin mind that plugin won't fix your problem, I don't know the cause or the solution ot your issue, I'm just suggesting best practices and debugging techniques to try and help identify what's going on – Tom J Nowell Commented Jul 4, 2020 at 18:19
 |  Show 5 more comments

1 Answer 1

Reset to default 1

Credit for this answer goes entirely to CodeMascot: Create and move terms for taxonomies

After troubleshooting, I believe the issue is because I initially created the custom taxonomy with the name Purpose, but then changed it to purpose at another time. Calling $object->taxonomy returns Purpose on the affected term, while all the others listed in the admin panel return purpose.

I strongly suspect that the taxonomy originally starting with a capital letter created a privileges issue, probably related to some to-lower-case method because some privileges were allowed. Once the taxonomy names matched perfectly, all issues were resolved.

After my troubleshooting determined that the taxonomy key of the term was incorrect, the question then became "How to change an existing term's taxonomy key"? The answer is that you must access the powerful and risky global $wpdb. This variable allows you to access and write the WordPress database directly. I added the following to the functions.php file, then refreshed a page two or three times, and the issue was resolved.

function the_dramatist_change_terms_taxonomy( $term_id, $future_taxonomy ){
    global $wpdb;
    $update = $wpdb->update(
        $wpdb->prefix . 'term_taxonomy',
        [ 'taxonomy' => $future_taxonomy ],
        [ 'term_taxonomy_id' => $term_id ],
        [ '%s' ],
        [ '%d' ]
    );
    return $update;
}

the_dramatist_change_terms_taxonomy(26,'purpose');

Now that the change has been made, I have removed the function from the functions.php file and $object->taxonomy returns purpose.

发布评论

评论列表(0)

  1. 暂无评论