• Hi,

    I am writing a plugin using OOP.

    I have a class (activated from the main plugin file by require_once) to create a custom post type and a hierarchal taxonomy and to populate the taxonomy with parent and children terms. The code below works as in the parents (carmakes) are added but when i try to add the children, only the 1st is added. On the taxonomy screen there are 4 pages now visible but they are blank. The database has the children added with the correct parent id……Anyone able to shed some light on this?

    class car_post_type {
    	var $dname= 'carmarket';
    	var $makes= null;
    	var $taxonomy = 'carmake';
    
    	public function __construct () {
    
    	$car= new base_posttype ('carmarket', array(
    	'has_archive' => TRUE,
    			'menu_position' => 11,
    			'rewrite' => array(
    				'slug' => $dname,
    				'with_front' => FALSE,
    			),
    			'supports' => array( 'title', 'editor', 'thumbnail', 'comments', 'custom-fields', 'revisions' ),
    			'menu_icon' => AD_URL . '/resources/img/caricon.png',
    ));
    
    	$car->add_taxonomy( 'location');
    
    	$car->add_taxonomy ($this->taxonomy, array(
    	'hierarchical' => true
    	));
    
    	$car->add_meta_box( 'Car_Info' , array(
    		'price' => 'text',
    		'engine_size' => 'text',
    		'fuel_type' => 'text',
    		'body_type' => 'text',
    		)
    	);
    
    	$this->makes = $this->assign_models();
    
    	add_action('init' , array( &$this ,'save_makes' ) );
    	add_action('init', array ( &$this, 'save_models'), 100 );
    
    	} // end construct
    
    	public function assign_models() {
    
    		$makes= array (
    		'Abarth'=> array('500'),
    		'Aixam' => array('Mega City'),
    		'Alfa Romeo' => array('146','147','156','159','166','Brera' ,'Giulietta' ,'GT' ,'GTV' ,'Mito','Spider'),
    		'Aston Martin' => array ('DB7','DB9','V8','V8'),
    		'Audi' => array('80','A1','A2','A3','A4','A5','A6','A7','A8','Allroad','Cabriolet','Q3','Q5','Q7','R8','RS4','RS5','S2','S3','S4','S5','S6','S8','TT'),
    		'Austin' => array('Healey','Mini'),
    		'BMW' => array('1 Series','2 Series','3 Series','4 Series','5 Series','6 Series','7 Series','i3','i8','X1','X3','X5','X6','Z4')
    
    		);
    
    		apply_filters('carmakes', $makes);
    
    		return $makes;
    
    	}
    
    	public function save_makes () {
    
    		foreach ($this->makes as $make => $models) {
    			$term='';
    			$terma='';
    			$termb='';
    			$term= term_exists( $make , 'carmake' );
    			if ($term == 0) {
    			echo $make;
    			echo $advert->post_type_name;
    			$y=wp_insert_term ($make, 'carmake' , array('parent' => 0) );
    			var_dump($y);
    			} 
    
    		}
    	}
    
    	public function save_models() {
    
    		foreach ($this->makes as $make => $models) {
    
    			foreach ($models as $model) {
    
    				$term= get_term_by ( 'name', $make , 'carmake' );
    				$termexists = get_term_by ('name' , $model, 'carmake' );
    
    				if ( $termexists ) {
    					$termexists = $termexists->term_id;
    				} else {
    					$termexists = null;
    				}
    
    				if( ! $termexists ) {
    					wp_insert_term ($model , 'carmake', array('parent'=>$term->term_id) );
    				}
    			}
    
    		}
    
    	}
    
    }
    $advert= new car_post_type;
  • The topic ‘Add terms to taxonomy’ is closed to new replies.