Search code examples
phpwordpresscategoriestaxonomy-terms

Create categories and multiple subcategories programmatically


This code show properly category in wordpress admin area. but not showing subcategory.

I Need to show 3 categories and 3 subcategories for each category?

This is what I would like to have for each category:

Category A

  • sub-category 1
  • sub-category 2
  • sub-category 3

I have add the following code in wordpress theme's functions.php file:

//create the main category
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',  
 ));`

Then for each sub-category:

wp_insert_term(

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

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

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

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

));

But I am getting an error:

Parse error: parse error, expecting `')'' in line 57 …

Corresponding to 'parent'=> term_exists( 'Category A', 'category' )['term_id'].

What I am doing wrong?


Solution

  • The problem is that you need to get the parent term id outside the function, to avoid the error. You can easily do it this way:

    $parent_term_a = term_exists( 'Category A', 'category' ); // array is returned if taxonomy is given
    $parent_term_a_id = $parent_term_a['term_id']; // get numeric term id
    
    // First subcategory
    wp_insert_term(
        'Sub-category 1', // the term 
        'category', // the taxonomy
        array(
            // 'description'=> 'Some description.',
            'slug' => 'sub-cat-1a',
            'parent'=> $parent_term_a_id
        )
    );
    
    // Second subcategory
    wp_insert_term(
        'Sub-category 2', // the term 
        'category', // the taxonomy
        array(
            // 'description'=> 'Some description.',
            'slug' => 'sub-cat-2a',
            'parent'=> $parent_term_a_id
        )
    );
    
    // Third subcategory
    wp_insert_term(
        'Sub-category 3', // the term 
        'category', // the taxonomy
        array(
            // 'description'=> 'Some description.',
            'slug' => 'sub-cat-3a',
            'parent'=> $parent_term_a_id
        )
    );
    

    Then you will use for other 2 groups of subcategories:

    // For subcategory group of Category B
    $parent_term_b = term_exists( 'Category B', 'category' );
    $parent_term_b_id = $parent_term_b['term_id'];
    
    // For subcategory group of Category C
    $parent_term_c = term_exists( 'Category C', 'category' );
    $parent_term_c_id = $parent_term_c['term_id'];
    

    … In the same way (taking care to have a unique slug for each subcategory, that mean at all 9 different subcategory slugs)

    Reference: