How do I restrict this function to a specific CPT?
-
I have this nifty function that automatically names a normal post type according to date, Id and author name etc. How would I edit this so that only the custom post types XYZ and ABC are renamed?
// auto post title by date,author name and post id add_action( 'wpuf_add_post_after_insert', 'wpuf_alter_title' ); add_action( 'wpuf_edit_post_after_update', 'wpuf_alter_title' ); function wpuf_alter_title ( $post_id){ $post_object=get_post($post_id); $post_date = $post_object->post_date; $format_date = DateTime::createFromFormat( 'Y-m-d H:i:s', $post_date ); $date_formatted = $format_date->format( 'Y-m-d' ); // Set correct to display here $post_author = $post_object->post_author; $author_name = get_the_author_meta( 'display_name', $post_author ); // Adjust as needed $my_post = array( 'ID' => $post_id, 'post_title' => '#' . $post_id . ' ' . $author_name . ' ' . $date_formatted // Change as needed ); // unhook this function so it doesn't loop infinitely remove_action('save_post', 'wpuf_alter_title'); wp_update_post( $my_post ); // re-hook this function add_action('save_post', 'wpuf_alter_title'); }
Viewing 4 replies - 1 through 4 (of 4 total)
Viewing 4 replies - 1 through 4 (of 4 total)
The topic ‘How do I restrict this function to a specific CPT?’ is closed to new replies.