Custom post title from hierarchical taxonomy
-
Hoping someone can put me out of my misery here…
I’m trying to set the post title of a custom post type (on submit) to include values from a custom taxonomy which is hierarchical.
I can get the page title to update, but the taxonomy terms are in alphabetical order.
I’ve tried to implement code that I’ve used elsewhere to display the taxonomy in hierarchical order, but it doesn’t seem to be working within my function.
Code to update page title:
function custom_post_type_title ( $post_id ) { global $wpdb; if ( get_post_type( $post_id ) == 'custom-post-type') { $taxonomy = 'taxonomy' //change as appropriate //$terms = wp_get_object_terms($post_id, $taxonomy); //$tax_1 = ' '.$terms[0]->name; //$tax_2 = ' '.$terms[1]->name; //$tax_3 = ' '.$terms[2]->name; $title = $tax_1.$tax_2.$tax3; $where = array( 'ID' => $post_id ); $wpdb->update( $wpdb->posts, array( 'post_title' => $title ), $where ); } } add_action( 'save_post', 'custom_post_type_title' );
The code above successfully writes or overwrites the post title on submit – just in alphabetical order and not hierarchical.
I’ve used the code below in my single custom post type template to display the taxonomy in the correct order and it works:
$rd_taxonomy = 'custom_taxonomy_name'; //taxonomy $rd_terms = wp_get_post_terms( $post->ID, $rd_taxonomy, array( "fields" => "ids" ) ); // getting the term IDs if( $rd_terms ) { $term_array = trim( implode( ',', (array) $rd_terms ), ' ,' ); $neworderterms = get_terms($rd_taxonomy, 'orderby=none&include=' . $term_array ); foreach( $neworderterms as $orderterm ) { $cpt_post_title[] = $orderterm->name; } } $parent = $cpt_post_title[0]; $child = $cpt_post_title[1]; $grandchild = $cpt_post_title[2]; echo $parent.' '.$child.' '.$grandchild;
Can anyone help get these 2 functions working?
Thanks in advance.
- The topic ‘Custom post title from hierarchical taxonomy’ is closed to new replies.