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( $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;
...这个变体不起作用。
$wpdb->query( $wpdb->prepare( $query ));对不起我的英语。
发布于 2022-01-18 14:46:55
我会使用你的友好邻里可湿性粉剂功能-看-这是一个!
在你的例子中,它应该是这样的:
...
$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" );
}
...这将正确地在添加到DB之前添加斜杠,这样在读取时就可以正确地删除它们。
如果您坚持要运行直接查询,准备你该死的书面陈述!
发布于 2022-04-02 21:15:38
我只是收到了同样的问题,从数据库接收帖子内容,然后wp_update_post()消耗大量的斜杠和混乱的数据库。
因此,实际上,在将post内容发送回数据库之前,它正确地应用了这一行:
$post_content = wp_slash($post_content);要更新post内容,正如其他人所提到的,您只需使用:
$my_post = array(
'ID' => $post->ID,
'post_content' => $post_content
);
wp_update_post( $my_post );https://wordpress.stackexchange.com/questions/401463
复制相似问题