I am trying to populate a child taxonomy from a parent taxonomy.
I previously got help from Minesh to populate a child taxonomy from a parent custom field. here: https://toolset.com/forums/topic/converting-data-from-a-checkbox-field-of-a-parent-cpt-to-populate-child-taxonomy/
While trying to get a MySQl request written to generate a table from my data I discovered that the way I was storing data in the Relevant field was causing problems... I was using multi checkboxes which stores the data in serialised form... which apparently doesn't work for mySQL.
The best solution to this problem is to change the way we are storing the State field for companies from Multi-checkboxes to a taxonomy. I have created a taxonomy for the parent "company-state" and want to populate the child taxonomy "Announcement-state". The taxonomy "company-state" is replacing the custom field "state" of "company-profile".
I tried unsuccessfully to revise the code that Minesh had written. see below. Not sure what I am doing wrong.... probably linked to the change from Custom field of parent to a taxonomy.
* Custom code to populate taxonomy - Announcement State
* to enable State by state rss feeds
*/
if ( $form_data['id'] == 1564) {
$parent_id = $_GET['parent_company-profile_id'];
// armed with parent id, get the state field of parent
$parent_state = get_post_meta( $parent_id, 'wpcf-company-state', true );
$parent_state_values = array_values($parent_state);
// array that binds parent state value with child taxonomy slug
$terms = array('western-australia'=>'western-australia','south-australia'=>'south-australia','victoria'=>'victoria','new-south-wales'=>'new-south-wales','queensland'=>'queensland','tasmania'=>'tasmania','northern-territory'=>'northern-territory',
'australian-external-territories'=>'australian-external-territories','overseas'=>'overseas','australian-capital-territory'=>'australian-capital-territory');
$set_terms = array();
foreach($parent_state_values as $k=>$v):
$term = get_term_by('slug',$terms[$v[0]] , 'announcement-state');
$set_terms[] = $term->term_id;
endforeach;
wp_set_object_terms($post_id, $set_terms, 'announcement-state');
}
Can you please help.