Last active
October 16, 2019 01:48
-
-
Save westcoastdigital/64e0bab53ef0c5414d36b1405c32997d to your computer and use it in GitHub Desktop.
Poplulate make from parent term of model
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function update_custom_terms($post_id) { | |
/** | |
* Define taxonomies | |
*/ | |
$make = 'make'; | |
$model = 'model'; | |
// only update terms if it's a vehicle-listings post | |
if ( 'vehicle-listings' != get_post_type($post_id)) { | |
return; | |
} | |
// don't create or update terms for system generated posts | |
if (get_post_status($post_id) == 'auto-draft') { | |
return; | |
} | |
/* | |
* Grab the parent term and populate the term | |
*/ | |
$terms = wp_get_post_terms($post_id, $model); | |
foreach ($terms as $term) { | |
if ($term->parent == 0) //check for parent terms only | |
wp_update_term($term->term_id, $make, array( | |
'name' => $term->name, | |
'slug' => $term->slug | |
) | |
); | |
return; | |
} | |
} | |
//run the update function whenever a post is created or edited | |
add_action('save_post', 'update_custom_terms'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment