Update Post Meta for a logged in user


I have created a form which collects information which I want to update a post with. The code I have works great, but instead of updating a post for the user, it creates a new one. Not sure what I am missing to get the current post to update instead of creating a new post. Here is the code I am using:

function update_role_and_post($record,$ajax_handler)
    $form_name = $record->get_form_settings('form_name');
    if ('Content' !== $form_name) {
    $current_user = wp_get_current_user($user_id);
    $current_user_id = get_current_user_id();
    $form_data = $record->get_formatted_data();

        // Remove old role
        $current_user->remove_role( 'subscriber' );

        // Add user to new role
        $current_user->add_role( 'editor' );

$title=get_user_meta( $current_user_id, 'title',true );
$subscription=get_user_meta( $current_user_id, 'subscription',true  );

    'subscription'=> $subscription

// Create post object
$my_post = array(
  'import_id'     => $current_user_id,
  'post_title'    => $title,
  'post_content'  => 'testing',
  'post_status'   => 'pending',
  'post_author'   => $current_user_id,
  'post_type'     => 'post',
  'meta_input'    => $confirm

wp_update_post( $my_post );
sodanotpop 8 months 2020-12-04T14:10:36-05:00 0 Answers 10 views 0

Leave an answer