-1

In my functions.php I have:

 global $wpdb;
 $wpdb->update( 
   $wpdb->posts, 
   array('post_content' => $newcontent), 
   array('ID' => $post_id) 
 );

this code updates the post_content in the WP DB with my $newcontent when I publish a post. In fact I can see the modified content inside the DB and of course I can see it with:

 echo get_post_field('post_content', $post_id);

The issue is that (after publish and update) the post editor continues to show the old content! In a few words... I need a way to force the editor to show the $newcontent, the one stored into the DB (after publish). Any ideas?

  • P.S. Of course if I exit from the post and then I go there again, I see the new content! – Augusto Feb 24 '16 at 16:44
  • Please provide full or at least sufficient context to your question to make it answerable. For example, show how the code snippet you posted actually is implemented. – Nicolai Feb 24 '16 at 17:18
  • 3
    Ah you want a live update? That functionality isn't present in WordPress Core, you'll need to build it yourself. Also, instead of changing the database directly, use wp_update_post so that the necessary caches and actions and filters are called, always avoid using direct database queries – Tom J Nowell Feb 24 '16 at 17:33
  • Please mark the answer that provided a solution as such, so the question does not stay open forever. Thanks. – kaiser Mar 12 '16 at 1:37
0

try wp update post function:

// Update post 37 for example
  $my_post = array(
  'ID'           => 37,
  'post_title'   => 'This is the post title.',
  'post_content' => 'This is the updated content.',
  );

// Update the post into the database
  wp_update_post( $my_post );

https://codex.wordpress.org/Function_Reference/wp_update_post

  • Yes, it was the wp_update_post!! Thank you Tom, Prosti and thank you Shady Alset you save me!! – Augusto Feb 24 '16 at 22:32
0

I am not quite sure what is your scenario, but I think you need to refresh the WordPress editor. If you did that try to update the post since it is cached. https://codex.wordpress.org/Function_Reference/wp_update_post

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.