I am using the below function to update the post meta from the front end. How can I add add best a textarea that updates the_content()
using wp_update_post()
?
if ( isset( $_POST['albums'] ) && wp_verify_nonce($_POST['albums'],'update_albums_postmeta') )
{ //if nonce check succeeds.
global $post;
$postid = $post->ID;
$data = $_POST['priceone'];
update_post_meta($postid,'_releasedate',$data);
$data = $_POST['pricetwo'];
update_post_meta($postid,'_amazonlink',$data);
}
–
Edit:
So this snippet is posting changes to the database, however when the page refreshes on-submit the old the_content()
is being shown. The post must manually be refreshed to see the changes.
Is my snippet malformed?
if ( isset( $_POST['drw_inventory'] ) && wp_verify_nonce($_POST['drw_inventory'],'update_drw_postmeta') )
{ //if nonce check succeeds.
global $post;
$data_content = $_POST['description'];
$my_post = array();
$my_post['ID'] = $post->ID;
$my_post['post_content'] = $data_content;
wp_update_post( $my_post );
}
It depends on where you’re using this. Is global $post giving you the specific post that you want to update? Your WordPress update post code looks right to me, but is the if-statement valid, and is $post->ID yielding the correct int?
You can override the
WP_Post
properties and send it towp_update_post
:I find it easier than an array.
I’m getting the same problem. My code is in the
single.php
file. I’m using the code from this article: Front end post editing using a formAfter clicking on submit, the code in
single.php
does runwp_update_post()
with the post ID being returned. Since this is being run from a template file, the wp_query has already been populated so the page still renders with the old post data. If I refresh without submitting, the new data is populated.I’m not sure if this is the best solution for it, but it works. After
wp_update_post()
is run, I overwrite the global$wp_query
variable using the same query that was run before this template file was called.I’ve tried calling
wp_reset_postdata()
andwp_reset_query()
instead but I’m guessing it is resetting to a cached copy because I still get the old post data.Another solution that worked was getting the current url using:
and then after
wp_update_post()
:The code for the $current_url was found here.