Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wordpress自定义字段在wp_update_post上丢失,带有扭曲_Wordpress_Custom Fields - Fatal编程技术网

Wordpress自定义字段在wp_update_post上丢失,带有扭曲

Wordpress自定义字段在wp_update_post上丢失,带有扭曲,wordpress,custom-fields,Wordpress,Custom Fields,我使用wp_update_post以编程方式从前端向帖子添加标题和标记。在这个过程中,我遇到了一个关于自定义字段的令人毛骨悚然的问题:在最初创建帖子时创建和填充的两个自定义字段中,有一个将其值删除,而另一个则完全可以 这是我用来创建帖子的代码的一部分: // Set the post ID so that we know the post was created successfully $post_id = wp_insert_post( array( 'comment

我使用wp_update_post以编程方式从前端向帖子添加标题和标记。在这个过程中,我遇到了一个关于自定义字段的令人毛骨悚然的问题:在最初创建帖子时创建和填充的两个自定义字段中,有一个将其值删除,而另一个则完全可以

这是我用来创建帖子的代码的一部分:

// Set the post ID so that we know the post was created successfully
$post_id = wp_insert_post(
    array(
        'comment_status'=>  'closed',
        'ping_status'   =>  'closed',
        'post_author'   =>  $author_id,
        'post_name'     =>  $slug,
        'post_status'   =>  'publish',
        'post_type'     =>  'custom'
    )
);

// If the post was created properly
if($post_id) {

    // Add meta/custom field data to post
    add_post_meta($post_id, 'custom_random_id', $randomId);
    add_post_meta($post_id, 'viewcount', '1');
然后,这是我用来更新标题和标签的代码:

// Continue if untampered
if($new_hashed_value == $_POST['hash']) {

    $updatePost = array();
    $updatePost['ID'] = $post_id;
    $updatePost['post_title'] = $title;
    $updatePost['tags_input'] = $tags;

    if(wp_update_post($updatePost)) {

        totallyUnrelatedStuff();
    }
我从wp_update_post中了解到可能会删除值,但在这种情况下,自定义字段“自定义随机id”始终保持完整,而“视图计数”始终删除其值

我试着改变它,使它:

if(wp_update_post($updatePost)) {

        update_post_meta($post_id, 'viewcount', '1');
    }
甚至:

if(wp_update_post($updatePost)) {
        delete_post_meta($post_id, 'viewcount');                                    
        add_post_meta($post_id, 'viewcount', '1');
    }
但是viewcount字段的值将继续被擦除

再者,再向我扔一把扳手

if(wp_update_post($updatePost)) {
        delete_post_meta($post_id, 'viewcount');                                    
        add_post_meta($post_id, 'new_field', 'new_value');
    }
工作完美

有人知道发生了什么事吗


谢谢

我也发生过类似的事情

wp\u update\u post
调用操作
save\u post
。由于您使用的是自定义帖子类型,因此可能需要在
save\u post
操作上运行一个自定义函数来保存元数据

问题是,当您调用
wp\u update\u post
时,用于保存元数据的自定义函数将这些值设置为空,因为它找不到要查找的数据(通常在
$\u post
中)

您需要添加一些额外的检查,以查看您的
save\u post
操作函数是否应该运行。在某种程度上,您需要测试它是从Wordpress的编辑屏幕调用的,还是从前端表单调用的

就我而言,这解决了问题:

function save_metadata($postid) {   
    global $post;  
    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return false;
    if ( !current_user_can( 'edit_page', $post->ID ) ) return false;
    if ( empty($post->ID) || get_post_type( $post->ID ) != 'post_type_here' ) return false;
    if ( !is_admin() ) return false;

    updateMyMetas();
}

我也发生过类似的事情

wp\u update\u post
调用操作
save\u post
。由于您使用的是自定义帖子类型,因此可能需要在
save\u post
操作上运行一个自定义函数来保存元数据

问题是,当您调用
wp\u update\u post
时,用于保存元数据的自定义函数将这些值设置为空,因为它找不到要查找的数据(通常在
$\u post
中)

您需要添加一些额外的检查,以查看您的
save\u post
操作函数是否应该运行。在某种程度上,您需要测试它是从Wordpress的编辑屏幕调用的,还是从前端表单调用的

就我而言,这解决了问题:

function save_metadata($postid) {   
    global $post;  
    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return false;
    if ( !current_user_can( 'edit_page', $post->ID ) ) return false;
    if ( empty($post->ID) || get_post_type( $post->ID ) != 'post_type_here' ) return false;
    if ( !is_admin() ) return false;

    updateMyMetas();
}

您是否使用任何缓存插件?您是否使用任何缓存插件?另一个选项是在函数save\u POST回调中的
调用之前检查
if(在
update\u user\u meta()之前设置($\u POST['custom\u random\u id'])
。另一个选项是在
update\u user\u meta()之前检查
if(设置($\u POST['custom\u random\u id']))
调用函数save\u post callback。