2

Creating a custom wordpress plugin where categories and sub-categories will be created based on data fetched from 3rd party API. For testing purpose I was using

wp_insert_term

It was working fine for single wordpress site. But this doesn't work in wordpress multisite. Considering I am getting started with wordpress plugin creation.

Here is a code below I was using in single wordpress site to create categories and sub-categories.

wp_insert_term(
// the name of the category
'Category A', 

// the taxonomy, which in this case if category (don't change)
'category', 

array(

// what to use in the url for term archive
'slug' => 'category-a',  
));
wp_insert_term(

    // the name of the sub-category
    'Sub-category Level 1', 

    // the taxonomy 'category' (don't change)
    'category',

    array(
    // what to use in the url for term archive
    'slug' => 'level-1', 

    // link with main category. In the case, become a child of the "Category A" parent  
    'parent'=> term_exists( 'Category A', 'category' )['term_id']

    ));
    wp_insert_term(

        // the name of the sub-category
        'Sub-category Level 2', 

        // the taxonomy 'category' (don't change)
        'category',

        array(
        // what to use in the url for term archive
        'slug' => 'level-2', 

        // link with main category. In the case, become a child of the "Category A" parent  
        'parent'=> term_exists( 'Sub-category Level 1', 'category' )['term_id']

        ));
        wp_insert_term(

            // the name of the sub-category
            'Sub-category Level 3', 

            // the taxonomy 'category' (don't change)
            'category',

            array(
            // what to use in the url for term archive
            'slug' => 'level-3', 

            // link with main category. In the case, become a child of the "Category A" parent  
            'parent'=> term_exists( 'Sub-category Level 2', 'category' )['term_id']

            ));

any help would be appreciated.

Adeel Tahir
  • 187
  • 2
  • 10

0 Answers0