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

register taxonomy - Custom taxonomies registered via plugin stop existing after function finishes running

programmeradmin5浏览0评论

I'm writing a plugin that retrieves information via an API and registers it as a term in a custom taxonomy.

But as soon as the script finishes running, the taxonomy doesn't exist anymore, and it gets recreated everytime the script runs, but since it doesn't exist anymore after that, the posts aren't affected.

Here's some relevant code:

public function ensure_place_taxonomy() {
    // Check if place is already a woocommerce taxonomy
    if (!taxonomy_exists( 'place' )) {
        // Create the "place" taxonomy
        $place_taxonomy = register_taxonomy(
            'place',
            'product',
            array(
                'label' => __( 'Place' ),
                'hierarchical' => true,
                'public' => true,
                'show_ui' => true,
                'show_in_nav_menus' => true,
                'show_tagcloud' => false,
                'show_in_quick_edit' => true,
                'show_admin_column' => true,
                'show_in_rest' => true
            )
        );
        // Register it as a woocommerce taxonomy
        register_taxonomy_for_object_type( 'place', 'product' );

        // Send an admin notice flagging that the place taxonomy was created again
        $this->send_admin_notice( 'info', 'place taxonomy was created again.' );

    } else {
        // Get the place taxonomy
        $place_taxonomy = get_taxonomy( 'place' );
    }

It creates the taxonomy and testing for taxonomy_exists() after that, works, but it's gone as soon as the script finishes. It doesn't show up in any page, testing for taxonomy_exists() anywhere else returns false and every time the script runs it warns that the taxonomy was created again.

Why isn't the taxonomy saving?

EDIT: I wasn't sure if it really gets saved, since every example just hooks it to the 'init' hook without even checking if it exists or not, so I did that as well:

    // Init the place custom taxonomy
    $this->loader->add_action( 'init', $plugin_admin, 'ensure_place_taxonomy' );

It fixed the problem of it not showing up in other pages, but the terms added and the products set up with the terms aren't being saved even then... What should I do?

发布评论

评论列表(0)

  1. 暂无评论