Blank title when duplicating a post
-
add_action( 'dp_duplicate_post', 'auto_generate_title_copy_post' ); function auto_generate_title_copy_post( $new_post_id ) { $post = get_post( $new_post_id ); $post_type = $post->post_type; if( $post_type == "ratings" ) { // get org tax $orgs = wp_get_post_terms($new_post_id, 'org'); $org_name = $orgs[0]->name; // get weightclass tax $wcs = wp_get_post_terms($new_post_id, 'weight-class'); $wc_name = $wcs[0]->name; // get date $date_data = get_field("rating_month", $new_post_id); if($date_data) { $date = DateTime::createFromFormat('Ymd', $date_data); $date_string = $date->format("m/Y"); } else { $date_string = "Resave to generate a new title"; } // update title $new_title = strtoupper($org_name . ": " . $wc_name . " - " . $date_string); // new post args array $post_update = array( 'ID' => $new_post_id, 'post_title' => $new_title ); // unhook this function so it doesn't loop infinitely remove_action('save_post', 'set_generated_post_titles'); // update the post wp_update_post( $post_update ); // re-hook this function add_action('save_post', 'set_generated_post_titles'); } }
I have a custom post type called ratings which has a generated post title based on taxonomies and other custom fields. I used the WP hook ‘save_post’ to generate the title based on information entered. When I duplicate a rating, the title is empty. Above you can see a copy of the code that I used in the ‘save_post’ hook, but used in the hook ‘dp_duplicate_post’. The only change was from $post_id to $new_post_id.
I have another custom post type that generates titles with only custom fields and the duplicate function seems to work fine without any additional code for the generated title. Does using taxonomies cause errors here?
- The topic ‘Blank title when duplicating a post’ is closed to new replies.