Hi Minesh,
I have a many-to-one relationship between post types 'Award Recipients' (many) 'Award' (one).
The slug/name of the post types is 'award-recipient' and 'award'.
I have a taxonomy called 'Academic Years' (academic-year) associated with the Award Recipient post type.
When creating an award-recipient, I would like to generate a title using the 'award' (relationship) and the 'academic-year' (taxonomy).
So, I hide the normal WordPress title field, and create the title from selections related ot the relationship and taxonomy.
I have created PHP snippets to auto-generate post titles, but only using normal text fields. As below. But, I cannot see how to access the relationship field value to insert into the title. What I would like is to use something similar to below, but using the relationship and taxonomy to generate the title.
----
function autogenerate_title( $post_id, $post ){
if ( 'student' == $post->post_type ) {
$fname = get_post_meta( $post_id, 'wpcf-first-name', true );
$lname = get_post_meta( $post_id, 'wpcf-last-name', true );
$new_title = $fname . " " . $lname;
$new_title = sanitize_text_field( $new_title );
$new_slug = sanitize_title( $new_title );
$args = array(
'ID' => $post_id,
'post_title' => $new_title,
'post_name' => $new_slug
);
// unhook this function so it doesn't loop infinitely
remove_action('save_post', 'autogenerate_title',30,2);
// update the post, which calls save_post again
wp_update_post( $args );
// re-hook this function
add_action('save_post', 'autogenerate_title',30,2);
}
}
add_action( 'save_post', 'autogenerate_title', 30, 2 );