admin管理员组

文章数量:1202393

foreach( $posts as $post ) {
    
                // Search for images and fill the missing dimensions in the current post content
                $post_content = $this->add_image_dimensions( $post->post_content );
    
                // Update the post content in the database only if the new $post_content it is not the same
                // as the current $post->post_content 
                if( $post_content != $post->post_content ) {
    
                    $query = "UPDATE " . $wpdb->prefix . "posts SET post_content = '" . $post_content . "' WHERE ID = " . $post->ID;
             
    
                    $wpdb->query( $query );
    
                }
    
                // Add a post meta to the current post to mark it as doone
                update_post_meta( $post->ID, 'image_dimensions_filled', 1 );

If there is a \ character in the text of the post, then it disappears. How to fix it?

Would that be correct?

...
                if( $post_content != $post->post_content ) {
                 $post_content = wp_slash($post_content);
                    $query = "UPDATE " . $wpdb->prefix . "posts SET post_content = '" . $post_content . "' WHERE ID = " . $post->ID;
...

This variant not work.

$wpdb->query( $wpdb->prepare( $query ));

Sorry for my English.

foreach( $posts as $post ) {
    
                // Search for images and fill the missing dimensions in the current post content
                $post_content = $this->add_image_dimensions( $post->post_content );
    
                // Update the post content in the database only if the new $post_content it is not the same
                // as the current $post->post_content 
                if( $post_content != $post->post_content ) {
    
                    $query = "UPDATE " . $wpdb->prefix . "posts SET post_content = '" . $post_content . "' WHERE ID = " . $post->ID;
             
    
                    $wpdb->query( $query );
    
                }
    
                // Add a post meta to the current post to mark it as doone
                update_post_meta( $post->ID, 'image_dimensions_filled', 1 );

If there is a \ character in the text of the post, then it disappears. How to fix it?

Would that be correct?

...
                if( $post_content != $post->post_content ) {
                 $post_content = wp_slash($post_content);
                    $query = "UPDATE " . $wpdb->prefix . "posts SET post_content = '" . $post_content . "' WHERE ID = " . $post->ID;
...

This variant not work.

$wpdb->query( $wpdb->prepare( $query ));

Sorry for my English.

Share Improve this question edited Jan 19, 2022 at 9:07 UserUser-name asked Jan 18, 2022 at 13:54 UserUser-nameUserUser-name 577 bronze badges 7
  • You should use wp_update_post() to properly update a post (e.g. so that post caches are updated), but if $post_content comes from a POST/GET input, then you should unslash the value when you pass it to the function - wp_update_post( [ 'post_content' => wp_unslash( $post_content ), ... ] ). – Sally CJ Commented Jan 19, 2022 at 7:50
  • Also, you should use wpdb::prepare() to prepare an SQL query for safe execution, or use esc_sql() to manually escape a variable that you'll use in your SQL queries. – Sally CJ Commented Jan 19, 2022 at 8:09
  • How will it look in code? – UserUser-name Commented Jan 19, 2022 at 8:45
  • Why doesn't it work $wpdb->query( $wpdb->prepare( $query )); – UserUser-name Commented Jan 19, 2022 at 9:03
  • Actually, the current answer is good - wp_insert_post() can also be called in place of wp_update_post() so long as you set the ID argument to a valid ID. So I'd upvote that answer, if I were you.. and I'm sure he could and would also help you with wpdb::prepare(). And sorry for asking, but why do you insist on using a custom query to update the post? – Sally CJ Commented Jan 19, 2022 at 9:23
 |  Show 2 more comments

2 Answers 2

Reset to default 0

I'd use your friendly neighbourhood WP functions - look - here's one!

wp_insert_post()

In your case it would be something like:

...
$post_content = $this->add_image_dimensions( $post->post_content );

$update_post = wp_insert_post(
    array(
        'ID'           => $post->ID,
        'post_content' => $post_content,
    )
);

// Don't forget error handling
if ( is_wp_error( $update_post ) ) {
    $error_code    = array_key_first( $update_post->errors );
    $error_message = $update_post->errors[ $error_code ][0];
    wp_die( "Post Update Error: Post ID: $post->ID  - $error_message" );
}
...

That will correctly add slashes before adding to the DB so they can be stripped correctly on read.

If you do insist on running direct queries, prepare your damned write statements!

I just got the same issue receiving post content from database and then wp_update_post() eating up slashes and messing up database.

So indeed it works correctly applying this line, before sending post content back to database:

$post_content = wp_slash($post_content);

to update post content, as others mentioned, you can simply use:

$my_post = array(
    'ID' => $post->ID,
    'post_content' => $post_content
);

wp_update_post( $my_post );

本文标签: How to fix the disappearance of the quotquot character when updating a post in the database