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 | Show 2 more comments2 Answers
Reset to default 0I'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
版权声明:本文标题:How to fix the disappearance of the "" character when updating a post in the database 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738648818a2104748.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
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:50wpdb::prepare()
to prepare an SQL query for safe execution, or useesc_sql()
to manually escape a variable that you'll use in your SQL queries. – Sally CJ Commented Jan 19, 2022 at 8:09$wpdb->query( $wpdb->prepare( $query ));
– UserUser-name Commented Jan 19, 2022 at 9:03wp_insert_post()
can also be called in place ofwp_update_post()
so long as you set theID
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 withwpdb::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