首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在更新数据库中的帖子时修复"\“字符的消失

如何在更新数据库中的帖子时修复"\“字符的消失
EN

WordPress Development用户
提问于 2022-01-18 13:54:10
回答 2查看 74关注 0票数 1
代码语言:javascript
运行
复制
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 );

如果文章文本中有一个\字符,那么它就消失了。怎么修呢?

那是对的吗?

代码语言:javascript
运行
复制
...
                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;
...

这个变体不起作用。

代码语言:javascript
运行
复制
$wpdb->query( $wpdb->prepare( $query ));

对不起我的英语。

EN

回答 2

WordPress Development用户

发布于 2022-01-18 14:46:55

我会使用你的友好邻里可湿性粉剂功能-看-这是一个!

wp_insert_post()

在你的例子中,它应该是这样的:

代码语言:javascript
运行
复制
...
$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之前添加斜杠,这样在读取时就可以正确地删除它们。

如果您坚持要运行直接查询,准备你该死的书面陈述

票数 0
EN

WordPress Development用户

发布于 2022-04-02 21:15:38

我只是收到了同样的问题,从数据库接收帖子内容,然后wp_update_post()消耗大量的斜杠和混乱的数据库。

因此,实际上,在将post内容发送回数据库之前,它正确地应用了这一行:

代码语言:javascript
运行
复制
$post_content = wp_slash($post_content);

要更新post内容,正如其他人所提到的,您只需使用:

代码语言:javascript
运行
复制
$my_post = array(
    'ID' => $post->ID,
    'post_content' => $post_content
);

wp_update_post( $my_post );
票数 0
EN
页面原文内容由WordPress Development提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://wordpress.stackexchange.com/questions/401463

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档