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 ('Confirm Premium Account' !== $form_name) {
return;
}
$current_user = wp_get_current_user();
$current_user->remove_role( 'subscriber' );
$current_user->add_role( 'editor' );
$form_data = $record->get_formatted_data();
$update=$form_data["Content"];
// EVERYTHING UP TO THIS POINT WORKS
// EVERYTHING BELOW DOES NOT WORK!
// Assign the ID from the current WP_User object to a var for use below
$user_id = $current_user->ID;
$confirm=array(
'subscription'=> $subscription // Wanting to use what is in the form and place it as the value for the upgraded meta-key
);
// Create array of arguments for our query
$args = array(
'author' => $user_id, // defined above
'post_status' => 'publish', // ignore deleted/drafted posts
'fields' => 'ids', // we only need the IDs
'post_type' => 'post',
'meta_input' => $confirm
);
// Query for posts by this author
$author_posts = new WP_Query( $args );
// WP_Query object contains lots of info; we only want the posts which will be an array if IDs based on our fields argument above.
$author_post_ids = $author_posts->posts; // array of IDs only
// Loop through each post by this author
foreach ( $author_post_ids as $post_id ) {
// On each loop iteration, update the post meta keys here
update_post_meta( $post_id, $meta_key, $meta_value );
}
}
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
Welcome to WPSE.
A few problems to note:
- $user_id is not defined so your call to wp_get_current_user() is passing a null value as a parameter.
$current_user = wp_get_current_user($user_id);. // $user_id is not defined
-
wp_get_current_user()does not accept parameters. See the documentation here: https://developer.wordpress.org/reference/functions/wp_get_current_user/ -
This line
$current_user->ID;does nothing. There is no ID method in the WP_User class.
You can simplify that section as follow:
$current_user = wp_get_current_user(); $current_user->remove_role( 'subscriber' ); $current_user->add_role( 'editor' ); // Assign the ID from the current WP_User object to a var for use below $user_id = $current_user->ID;
Regarding post update vs creation, wp_update_post() expects an ID for the post you are updating. You appear to pass the user ID as $my_post['import_id'] which is not valid.
Are you attempting to update the user object or a post with user information?
EDIT: OP wishes to update the meta key/value of each post for the current author.
The code above solves the user role challenge and the following is a model for updating the post meta for each post that specific user has authored.
Note: update_post_meta() needs the post ID, the meta_key (the slug for the field) and the meta_value for each field you are updating.
https://developer.wordpress.org/reference/functions/update_post_meta/
// Create array of arguments for our query
$args = array(
'author' => $user_id, // defined above
'post_status' => 'publish', // ignore deleted/drafted posts
'fields' => 'ids' // we only need the IDs
);
// Query for posts by this author
$author_posts = new WP_Query( $args );
// WP_Query object contains lots of info; we only want the posts which will be an array if IDs based on our fields argument above.
$author_post_ids = $author_posts->posts; // array of IDs only
// Loop through each post by this author
foreach ( $author_post_ids as $post_id ) {
// On each loop iteration, update the post meta keys here
update_post_meta( $post_id, $meta_key, $meta_value );
}
EDIT adding actual meta key and value specifics for OP.
function update_role_and_post( $record ) {
if ( 'Confirm Premium Account' !== $record->get_form_settings( 'form_name' ) ) {
return;
}
// Update current user role to editor
$current_user = wp_get_current_user();
$current_user->remove_role( 'subscriber' );
$current_user->add_role( 'editor' );
// Create array of arguments for our query
$args = array(
'author' => $current_user->ID,
'post_status' => 'publish',
'fields' => 'ids'
);
// Query for posts by this author
$author_posts = new WP_Query( $args );
// Parse the post IDs from the rest of the WP_Query object
$author_post_ids = $author_posts->posts;
// Loop through each post by this author and update to premium
foreach ( $author_post_ids as $post_id ) {
update_post_meta( $post_id, 'subscription', 'premium' );
}
}
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0