WordPress.org

Ready to get started?Download WordPress

Forums

Create Hiearchical Taxonomy Terms (1 post)

  1. miradev
    Member
    Posted 3 years ago #

    I'm trying to create hierarchical taxonomy terms on activation of a plugin. I'm just creating 1 parent and 1 child. The terms are being inserted into the database.

    My problem is updating the 'cache' blogoption that contains the hierachy (children), which gets updated by calling clean_term_cache().

    add_action( 'init', 'create_user_location_tax', 0 );
    function create_user_location_tax(){
      global $wpdb;
      register_taxonomy( 'location', 'users', array('hierarchical' => true,  'show_ui' => true, 'query_var' => true, 'label' => __('Where are you')) );
    
      $dummycountry = 'USA';
      $dummystate   = 'KENTUCKY';
      if( taxonomy_exists('location') ){
        $tax_location_terms = get_terms( 'location', 'get=all&hide_empty=false');
        if( count($tax_location_terms) < 1 ){
          $tax_country  = wp_insert_term($dummycountry,  'location');
          $parent       = $tax_country['term_id'];
          $tax_state    = wp_insert_term($dummystate, 'location', array('parent'=>$parent));
          $child        = $tax_state['term_id'];
          clean_term_cache(array($parent), 'location');
        }
      }
    
      $sql    = "SELECT option_value FROM $wpdb->options WHERE option_name = 'location_children'";
      $result = $wpdb->get_col($sql);
      error_log('term cache: '.$result[0]);
    }

    Where is the right place to call clean_term_cache()?
    How do I store and pass the right IDs to it?

Topic Closed

This topic has been closed to new replies.

About this Topic