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) 
if(!current_user_can('edit_post', $post_id))

$term1 = wp_get_post_terms($post_id, 'name', array('fields' => 'names'));
$term2 = wp_get_post_terms($post_id, 'band', array('fields' => 'names'));

$terms = $term1;


$title = false; 
foreach($terms as $term) 
        $parent = get_term($term->parent, 'name'); 
        $title = $term->name.' '.$parent->name;
/*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(
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?

  • Welcome to WPSE Lars! Can you explain exactly 'what isn't working'? What steps have you already taken to debug the code? – Stephen Harris Nov 9 '12 at 17:07
  • 1
    Hi @StephenHarris, I've updated the question to be able to get a better answer. Thanks for all the edits sofar, great community! – Lars van Peij Nov 12 '12 at 11:38

Updated Answer:

You're trying to mix a lot of things that you don't understand, by using the $args in wp_get_post_terms you don't need to run the foreach to hunt for child terms (is your custom taxonomy even hierarchical anyway?). As a catch-all you can just implode the whole list and get the same result (but without seeing/knowing your schema I can't be sure it's actually what you want.)

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;

    $names = wp_get_post_terms($post_id, 'name', array('fields' => 'names'));
    // $names == array( 0 => 'name1'[, 1 => 'name2'[, 2 => ...]])
    $bands = wp_get_post_terms($post_id, 'band', array('fields' => 'names'));
    // $bands == array( 0 => 'band1'[, 1 => 'band2'[, 2 => ...]])

    // collapse parent and child terms
    $name = implode(' ', $names);
    // $name == "name1[ name2[ ...]]"
    $band = implode(' ', $bands);
    // $band == "band1[ band2[ ...]]"

    if ($name OR $band) {
        // concat name and band, use trim to clean the string if one is missing
        $title = trim(implode(' ', array($name, $band)));
        // $title == "name1[ name2[ ...]] band1[ band2[ ...]]"
        // disable and reenable hook from within to avoid a loop
        remove_action('save_post', 'update_term_title');
        $update = array(
            'ID' => $post_id,
            'post_name' => sanitize_title_with_dashes($title),
            'post_title' => $title,
        add_action('save_post', 'update_term_title');

Normally I wouldn't suggest this method because of the disable/reenable hack, there is a filter for post data called wp_insert_post_data meant for altering the post data prior to the DB UPDATE/INSERT, but you'd probably have to understand quite a bit more to get past figuring out how to reference the taxonomy items that haven't yet been saved. The above method is technically quite expensive as it requires saving a post, requesting the post and then saving the post again

Old Answer:

wp_get_post_terms() returns an array, not a string. wp_get_post_terms docs

You need to treat $term1 and $term2 as arrays.

$term1 = wp_get_post_terms($post_id, 'name', array('fields' => 'names'));
$term2 = wp_get_post_terms($post_id, 'band', array('fields' => 'names'));
# this assumes the first term in the taxonomy is the one you want
$terms = array_pop($term1) . array_pop($term2);
  • or for fun: $terms = implode(' - ', wp_get_object_terms($post_id, array('band', 'name'), array('fields' => 'names')); – totels Nov 9 '12 at 18:13
  • Thanks, but I've updated the question so it's easier to answer. – Lars van Peij Nov 12 '12 at 11:39
  • Thanks @totels, it now says unexpected ; on the line $title = trim(implode(' ', array($name, $band)); Quick answer; there is no hierarchy in both taxonomies. – Lars van Peij Nov 12 '12 at 13:22
  • Ooops, missing a closing parens ), fixed. – totels Nov 12 '12 at 13:31
  • Works like a charm! I've made one edit to get a dash in between the band name and song title. Thanks a lot @totels! $title = trim(implode(' ', array($band, '-', $name))); – Lars van Peij Nov 12 '12 at 13:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.