UPDATED: Save a custom_field value when automatically creating a post using wp_insert_post

The following code automatically creates a new post for a user when they are registered in a custom post type (jt_cpt_team) for team members.

The final missing part of the puzzle is to save the $user_id within a custom_field (jt_user_id) in the newly created post.

Any ideas on how to achieve this? Thanks in advance for your help.

class team_functions {    public function __construct() {      add_action('user_register', array($this, 'create_authors_page'), 999);    }    public function create_authors_page( $user_id ) {      $the_user      = get_userdata( $user_id );     $new_user_name = $the_user->user_login;     $fname         = $the_user->first_name;     $lname         = $the_user->last_name;     $PostSlug      = $user_id;     $PostGuid      = home_url() . "/" . $PostSlug;      $my_post = array( 'post_title'   => $fname . '' . $lname,                       'post_type'    => 'jt_cpt_team',                       'post_content' => '',                       'post_status'  => 'publish',                       'post_theme'   => 'user-profile',                       'guid'         => $PostGuid );      $NewPostID = wp_insert_post( $my_post ); // Second parameter defaults to FALSE to return 0 instead of wp_error.      $key   = 'jt_user_id'; // Custom Field Key     $Value = $user_id;     // User ID is the custom_field value      update_post_meta( $NewPostID, $Key, $Value );      return $NewPostID;    }   } 

Props to Felipe for getting me this far btw :)


Solved: There was different capitalization on the variable $key

($Key vs $key)

Always the simple things you miss eh? :)

Category: custom post types Time: 2016-07-31 Views: 5

Related post

iOS development

Android development

Python development

JAVA development

Development language

PHP development

Ruby development


Front-end development


development tools

Open Platform

Javascript development

.NET development

cloud computing


Copyright (C) avrocks.com, All Rights Reserved.

processed in 0.164 (s). 12 q(s)