I have two custom taxonomies; "name" and band". The first represents the name of the song, the second represents the band that made the song.
I want to automatically save a combination of those two custom taxonomies in the post title upon saving.
So in the following example: name = "Little Lion Man" band = "Mumford and sons" .. I want the post title to become "Mumford and sons - Little Lion Man"
The code I now have (and works!) is based on this post. However, what this code does is only get one custom taxonomy (in this case the song name, or 'name'). I want to put two custom taxonomy terms in the post title.
This is the code I have now, and I don't know where to begin. I'm not an expert on php.
add_action('save_post', 'update_term_title');
function update_term_title($post_id)
{
if(defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
return;
if(!current_user_can('edit_post', $post_id))
return;
$term1 = wp_get_post_terms($post_id, 'name', array('fields' => 'names'));
$term2 = wp_get_post_terms($post_id, 'band', array('fields' => 'names'));
$terms = $term1;
if(empty($terms))
return;
$title = false;
foreach($terms as $term)
{
if($term->parent)
{
$parent = get_term($term->parent, 'name');
$title = $term->name.' '.$parent->name;
break;
}
}
/*Default to first selected term name if no children were found*/
$title = $title ? $title : $terms[0]->name;
/*We must disable this hook and reenable from within
if we don't want to get caught in a loop*/
remove_action('save_post', 'update_term_title');
$update = array(
'ID'=>$post_id,
'post_name'=>sanitize_title_with_dashes($title),
'post_title'=>$title
);
wp_update_post($update);
add_action('save_post', 'update_term_title');
}
Does anyone have a suggestion on how to make this work? It should be a minor modification of the code. Maybe make two loops?
