Search code examples
titlehierarchytaxonomy

Update post title with hierarchical taxonomy - need grandchildren to also pull through


I've spent a while getting to this point but I could do with some help.

I have a function that sets a custom post title on save to include a hierarchical taxonomy. So far, parent and child is pulling through, but not the grandchild.

I have tried many variations of code, and my main issue is that I need to grab the current terms (on save) rather than grabbing the terms currently saved against the post.

Can anyone help get the grandchildren to add to the post title based on the code below? Terms must be in hierarchical order (parent>child>grandchild):

Many thanks in advance.

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;

    $terms = wp_get_post_terms($post_id, 'TAXONOMY');

    if(empty($terms))
        return;

    $title = false;
    foreach($terms as $term)
    {
        if($term->parent)
        {
            $parent = get_term($term->parent, 'TAXONOMY');
            $title = $parent->name.' '.$term->name;
            break;
        }
    }
    /*Default to first selected term name if no children were found*/
    $title = $title ? $title : $terms[0]->name;

    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');
}

Solution

  • here's a modified version that should do the trick:

    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;
    
    $terms = wp_get_post_terms($post_id, 'TAXONOMY');
    
    if(empty($terms))
        return;
    
    $parent = $child = $grandchild = false;
    $parent_id = $child_id = $grandchild_id = 0;
    
    // Find parent
    foreach($terms as $term)
    {
        if(!$term->parent)
        {
            $parent = $term->name;
            $parent_id = $term->term_id;
            break;
        }
    }
    // Find child
    foreach($terms as $term)
    {
        if($term->parent == $parent_id)
        {
            $child = $term->name;
            $child_id = $term->term_id;
            break;
        }
    }
    // Find grandchild
    foreach($terms as $term)
    {
        if($term->parent == $child_id)
        {
            $grandchild = $term->name;
            $grandchild_id = $term->term_id;
            break;
        }
    }
    
    $title = trim( $parent . ' ' . $child . ' ' . $grandchild );
    
    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');
    }