Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save richardscholtens/b87ab3496e2460188fff26ae8fc3e27f to your computer and use it in GitHub Desktop.
Save richardscholtens/b87ab3496e2460188fff26ae8fc3e27f to your computer and use it in GitHub Desktop.
Just a way to programmatically add hierarchical taxonomies in WordPress
// For one level deep Wordpress nested terms.
$taxonomy_term = 'example';
$related_terms = array(
__( 'Parent' ) => array(
__( 'Child 1' ),
__( 'Child 2' ),
),
__( 'Parent 2') => array(
__( 'Child 3' ),
__( 'Child 4' ),
),
);
foreach ($related_terms as $key => $term) {
wp_insert_term(
(string)$key,
$taxonomy_term,
array(
'slug' => sanitize_title_with_dashes((string)$key),
)
);
$parent_term = term_exists( $key, $taxonomy_term );
$term_id = $parent_term['term_id'];
foreach ($term as $term_value) {
wp_insert_term(
$term_value,
$taxonomy_term,
array(
'slug' => sanitize_title_with_dashes( $term_value ),
'parent'=> $term_id
)
);
}
}
// For three level deep Wordpress nested terms.
$taxonomy_term = 'example';
$related_terms = array(
__( ' Parent 1 ' ) => array(
__( ' Child 11 ' ),
__( ' Child 12 ' ),
__( ' Child 13' ),
),
__( 'Parent 2') => array(
__( ' Child parent 21' ) => array(
__( ' Child 211 ' ),
__( ' Child 212 ' ),
__( ' Child 213' ),
__( ' Child 214' ),
),
),
__( ' Parent 3') => array(
__( ' Child parent 31' ) => array(
__( ' Child 311 ' ),
__( ' Child 312 ' ),
__( ' Child 313 ' ),
__( ' Child 314 ' ),
__( ' Child 315 ' ),
),
__( ' Child parent 32 ' ) => array(
__( ' Child 321 ' ),
__( ' Child 322 ' ),
__( ' Child 323 ' ),
__( ' Child parent 324 ' ) => array(
__( ' Child 3241 ' ),
__( ' Child 3242 ' ),
),
),
),
);
foreach ($related_terms as $key => $term) {
wp_insert_term(
(string)$key,
$taxonomy_term,
array(
'slug' => sanitize_title_with_dashes((string)$key),
)
);
$parent_term = term_exists( $key, $taxonomy_term );
$term_id = $parent_term['term_id'];
foreach ($term as $child => $term_second) {
if (!is_array($term_second)) {
$child = $term_second;
}
wp_insert_term(
(string)$child,
$taxonomy_term,
array(
'slug' => sanitize_title_with_dashes( (string)$child ),
'parent'=> $term_id
)
);
if (is_array($term_second)) {
$parent_term_second = term_exists( $child, $taxonomy_term );
$term_id_second = $parent_term_second['term_id'];
foreach ($term_second as $child_second => $term_third) {
if (!is_array($term_third)) {
$child_second = $term_third;
}
// print_r($term_child_second);
wp_insert_term(
$child_second,
$taxonomy_term,
array(
'slug' => sanitize_title_with_dashes( (string)$child_second ),
'parent'=> $term_id_second
)
);
if (is_array($term_third)) {
$parent_term_third = term_exists( $child_second, $taxonomy_term );
$term_id_third = $parent_term_third['term_id'];
foreach ($term_third as $child_third) {
wp_insert_term(
$child_third,
$taxonomy_term,
array(
'slug' => sanitize_title_with_dashes( (string)$child_third ),
'parent'=> $term_id_third
)
);
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment