Update Post Meta for a logged in user

Question

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) {
        return;
    }
    
    $current_user = wp_get_current_user($user_id);
    $current_user->ID;
    $current_user_id = get_current_user_id();
    
    $form_data = $record->get_formatted_data();
    $update=$form_data["Content"]; 
    
    

        // 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  );


$confirm=array( 
    $current_user_id,
    '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 );
}   
0
sodanotpop 2 months 0 Answers 10 views 0

Leave an answer