【问题标题】:Wordpress wp_update_post() inserts empty post_titleWordpress wp_update_post() 插入空的 post_title
【发布时间】:2016-02-19 05:55:33
【问题描述】:

我正在使用 Wordpress 主题 WPJobus,我有必要修改简历提交和简历编辑的工作流程。

提供这些操作的函数像往常一样位于文件/mytheme/functions.php中

wpjobusSubmitResumeForm()
wpjobusEditResumeForm()

在他们两个上,我都删除了在每次编辑发生时将帖子状态切换为“草稿”的整个代码部分,因为我需要让新用户可以插入他们自己的帖子(简历)并在没有管理员监督。

无论如何,我进行了所有更改,一切正常,除了带有 posts.post_title 字段的函数 wp_update_post()!

代码如下所示:

$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

【问题讨论】:

  • 为什么将post_name设置为更新中的ID?
  • 问题可能是主题将自己插入到进程中,但由于它是高级主题,因此无法根据您问题中的信息进行调试。
  • 嗨,因为它是 WPJobus 主题的默认特性。创建新的简历后,它会生成一个如下所示的 URL:www.mywebproj.com/resume/$resumeID 检查一下:alexgurghis.com/themes/wpjobus/resume/565
  • 你的意思是你需要整个源代码来分析它并检查可能的错误......是的,不幸的是这是一个糟糕的问题:(
  • 我会搜索“post_title”的所有主题代码,看看它是否正在修改它。它可能正在使用pre_post_update 钩子。

标签: php wordpress


【解决方案1】:

谢谢你的回答doublesharp!

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

导致问题的函数是第 636 行的 filter_handler()。

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

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

现在,如果我运行上面的脚本,wp_post_update() 就像 Resume Post Type 的魅力一样(我猜对 Company 和 Job Post Types 也是如此)。

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

这里是函数的代码:

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')) {

确定问题出在该函数内,因此必须对其进行任何可能的更正。

【讨论】:

  • 这个答案应该是对您的问题的真正编辑,因为它提供了更多信息。有关删除主题添加的过滤器的正确方法,请参阅我的更新答案。
【解决方案2】:

现在您已经提供了更多信息,更改此行为的正确方法是删除您的主题添加的过滤器,这将使您能够控制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 进行调试,您可以打印用于更新posts 表的$data 数组的内容,以及包含您传入的原始字段的$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 );

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-02-05
    • 1970-01-01
    • 1970-01-01
    • 2015-03-02
    • 1970-01-01
    • 2019-06-11
    • 1970-01-01
    • 2020-12-13
    相关资源
    最近更新 更多