Wordpress wp_update_post()插入空的post_title

时间:2015-11-17 18:11:49

标签: php wordpress

我正在使用Wordpress主题WPJobus,我有必要修改Resume Submission和Resume Editing的工作流程。

为这些操作提供的功能通常位于文件/mytheme/functions.php中

wpjobusSubmitResumeForm()
wpjobusEditResumeForm()

在他们两个上我删除了整个代码部分,将帖子状态切换为" Draft"在每次编辑时,由于我需要向新用户提供插入自己的帖子(简历)并在没有管理员监督的情况下发布的可能性。

无论如何,我做了所有更改,一切正常,除了功能wp_update_post()与 posts.post_title 字段!

代码如下所示:

$post_information = array(
    'ID' => $td_current_post,
    'post_title' => $_POST['fullName'],
    'post_content' => strip_tags($_POST['postContent']),
    'post_type' => 'resume',
    'post_name' => $td_current_post,
    'comment_status' => 'closed',
    'ping_status' => 'closed',
    'post_status' => 'publish'
);

$td_post_id = wp_insert_post($post_information);

wp_update_post( 
            array(
                'ID' => $td_post_id,
                'post_name' => $td_post_id 
            ) 
);

INSERT工作正常但是当我调用UPDATE时,这对post_name字段是绝对必要的,post_name正确更新,而post_title值被删除。

即使我尝试在wp_post_update数组中传递' post_title' => $ _ POST [' fullName'],我也有同样的效果。

有人有同样的问题吗?

我也发现了这一点,但它根本没有帮助: wp_update_post inserts empty title and content

2 个答案:

答案 0 :(得分:1)

现在您已提供了更多信息,更改此行为的正确方法是删除主题添加的过滤器,这样您就可以控制post_title

// add an action after the theme has loaded - wp_loaded is safe
add_action( 'wp_loaded', function(){
    // check for permissions?
    // if ( !current_user_can( 'administrator' ) ) return;

    // remove the filter that the theme added
    remove_filter( 'wp_insert_post_data' , 'filter_handler' , '99', 2 );
} );

如果无法访问主题的源代码,则无法说明错误的来源,但您应该能够使用pre_update_posts操作进行调试,并可能更正行为使用wp_insert_post_data过滤器。

要使用pre_update_posts进行调试,可以打印用于更新$data表的posts数组的内容,以及包含$postarr数组的内容您传入的原始字段(无法使用过滤器修改)。

// print the $data and $postarr to the error log when inserting/updating
add_action( 'pre_update_posts', function( $data, $postarr ){
    error_log( 'The $data array contains ' . print_r( $data, true ) );
    error_log( 'The $postarr array contains ' . print_r( $postarr, true ) );
}, 100, 2 );

修改正在插入/更新的帖子数据的最后一个可用位置是wp_insert_post_data过滤器。通过比其他所有内容更晚地附加到此过滤器,您在数组值中具有最终发言权。

add_filter( 'wp_insert_post_data', function( $data, $postarr ){
    // you could also log here for debugging
    // error_log( 'The $data array contains ' . print_r( $data, true ) );
    // error_log( 'The $postarr array contains ' . print_r( $postarr, true ) );

    // set the title to whatever was initially passed in
    $data['post_title'] = $postarr['post_title'];

    // return the modified data so that it can be used in the update
    return $data;
}, 999, 2 );

答案 1 :(得分:1)

感谢您的回答doublesharp!

我终于找到了问题的根源,只是逐行仔细阅读functions.php文件。

引起问题的函数是第636行的filter_handler()。

在这种情况下对我来说,评论与此函数相关的钩子add_filter()就足够了,我实现了目标。

add_filter( 'wp_insert_post_data' , 'filter_handler' , '99', 2 );

现在,如果我运行上面的脚本, wp_post_update()就像一个带有恢复帖子类型的魅力(我猜也是公司和职位类型)。

我会做一些更多的测试,如果我遇到一些意想不到的问题,我会更新这个帖子。

这里是函数的代码:

function filter_handler( $data , $postarr ){
global $post, $id;

if(('job' == $data['post_type'] && isset($data['post_type'])) or ('company' == $data['post_type'] && isset($data['post_type'])) or ('resume' == $data['post_type'] && isset($data['post_type']))) {

    $id = $postarr['ID'];

    if($id) {

        $title = $_POST['ID'];
        $wpjobus_title = esc_attr(get_post_meta($title, 'wpjobus_post_title',true));

        if(!empty($wpjobus_title)) {
            $data['post_title'] = $wpjobus_title;
        } else {
            $data['post_title'] = $title;
        }

        $data['post_name'] = $title;
    }

}

return $data; }

另一个解决方案应该是强制调用 filter_handler()仅当当前用户是管理员时使用相关的控制器':

if (current_user_can('administrator')) {

确定问题位于该功能内,因此必须对其进行任何可能的修正。