Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function wpse_203349_copy_post_meta( $post_ID, $post, $update ) {
- if ( $post->post_type !== 'post' )
- return;
- $users_custom_posts = get_posts(
- array(
- 'posts_per_page' => 1,
- 'post_author' => $post->post_author,
- 'post_type' => 'custom_post_type',
- )
- );
- if ( ! $users_custom_posts )
- return; // This author doesn't currently have any custom posts
- $fields = get_post_custom( $users_custom_posts[0]->ID );
- foreach ( $fields as $field => $value ) {
- if ( $field[0] !== '_' && ( empty( $value[0] ) || ! is_array( $value[0] ) ) ) // Ignore "private" fields (prefixed with an underscore or serialized data)
- add_post_meta( $post_ID, $field, empty( $value[0] ) ? '' : $value[0], true /* Unique */ ); // If the field already exists, it won't be overwritten, unlike update_post_meta()
- }
- }
- add_action( 'wp_insert_post', 'wpse_203349_copy_post_meta', 10, 3 );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement