admin管理员组文章数量:1390503
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 );
}
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 );
}
Share
Improve this question
edited Jul 7, 2011 at 3:02
MTT
asked Jul 7, 2011 at 1:28
MTTMTT
3,58612 gold badges47 silver badges74 bronze badges
2
- where are you using this code? – Hameedullah Khan Commented Jul 7, 2011 at 5:56
- the top of content-pt-album.php, pt being post type. Its modeled after twentyeleven. – MTT Commented Jul 7, 2011 at 13:11
3 Answers
Reset to default 1It 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 to wp_update_post
:
/** @var WP_Post $post */
$post = get_post( 123 );
$post->post_content = "Some other content";
wp_update_post( $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 form
After clicking on submit, the code in single.php
does run wp_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.
global $wp_query;
if ('POST' == $_SERVER['REQUEST_METHOD'] && !empty($_POST['post_id']) && !empty($_POST['post_title']) && isset($_POST['update_post_nonce']) && isset($_POST['post_content'])) {
$post_id = $_POST['post_id'];
$post_type = get_post_type($post_id);
$capability = ('page' == $post_type) ? 'edit_page' : 'edit_post';
if (current_user_can($capability, $post_id) && wp_verify_nonce($_POST['update_post_nonce'], 'update_post_' . $post_id)) {
$post = array(
'ID' => esc_sql($post_id),
'post_content' => wp_kses_post($_POST['post_content']),
'post_title' => wp_strip_all_tags($_POST['post_title'])
);
$result = wp_update_post($post, true);
if (is_wp_error($result)){
wp_die('Post not saved');
}
$wp_query = new WP_Query($wp_query->query); //resets the global query so updated post data is available.
} else {
wp_die("You can't do that");
}
}
I've tried calling wp_reset_postdata()
and wp_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:
global $wp;
$current_url = home_url(add_query_arg(array(),$wp->request));
and then after wp_update_post()
:
wp_redirect($current_url);
The code for the $current_url was found here.
本文标签: wpupdatepost() example how to update thecontent in a textarea
版权声明:本文标题:wp_update_post() example... how to update the_content in a textarea? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744590917a2614486.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论