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

Question

I’m writing a plugin that retrieves a information via an API and register 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 everytime the script runs it warns that the taxonomy was created again.

Why isn’t the taxonomy saving?

0
Miguel Vieira 1 year 2022-01-29T16:32:39-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse