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 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.      return $NewPostID;    }   } 

Props to Felipe for getting me this far btw :)

Replay

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

search

Front-end development

Database

development tools

Open Platform

Javascript development

.NET development

cloud computing

server

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

processed in 0.140 (s). 13 q(s)