为什么我的自定义表单保存时标题和辅助字符都是‘AutoDraft’?

时间:2014-01-25 作者:Ben Wainwright

我目前正在从头开始编写一个自定义主题,它利用了许多自定义帖子类型。我目前正在尝试为他们创建自定义张贴表单。我已经隐藏了所有正常表单字段,并希望标题/段塞基于其中一个自定义字段填充。到目前为止,元信息正在保存fin,但标题/slug始终是“autodraft”。有人知道我该怎么解决这个问题吗?

我已经在下面发布了相关的代码片段。

来自函数。php

include_once( \'init/custom_post_types.php\' );
来自init/custom\\u post\\u类型。php

function save_custom_post_form( $post ) {
    if( file_exists( dirname( __FILE__  ) . \'/../submission_handling/\' . $_POST [\'post_type\'] . \'.php\' ) )
        include_once( dirname( __FILE__  ) . \'/../submission_handling/\' . $_POST [\'post_type\'] . \'.php\' );
}
add_action( \'save_post\', \'save_custom_post_form\');
提交处理/夹具。php(当$\\u POST[\'POST\\u type\']==“fixture”时调用)

<?php
if(basename(__FILE__) == basename($_SERVER[\'PHP_SELF\'])){exit();}

// save metainformation from custom form. This code works fine

$fixturedate = strtotime( $_POST[\'fixture-date\'] );
update_post_meta($post, \'fixture-date\', $fixturedate);
$fixturekickoff = esc_attr( $_POST[\'fixture-kickoff-time\'] );    
update_post_meta($post, \'fixture-kickoff-time\', $fixturekickoff);
$fixtureoppteam = esc_attr( $_POST[\'fixture-opposing-team\'] );
update_post_meta($post, \'fixture-opposing-team\', $fixtureoppteam);
$fixturepat = esc_attr( $_POST[\'fixture-player-arrival-time\'] );
update_post_meta($post, \'fixture-player-arrival-time\', $fixturepat);
$fixtureoppteamurl = esc_attr( $_POST[\'fixture-opposing-team-website-url\'] );       
update_post_meta($post, \'fixture-opposing-team-website-url\', $fixtureoppteamurl);
$fixtureaddy = esc_attr( $_POST[\'fixture-address\'] );
update_post_meta($post, \'fixture-address\', $fixtureaddy);
$fixface = esc_attr( $_POST[\'fixture-facebook-event\'] );
update_post_meta($post, \'fixture-facebook-event\', $fixface);

// Save title and slug into new post. This doesn\'t seem to work

if ( ! wp_is_post_revision( $post ) ){
    $posttitle = array (
         \'ID\' => $post,
         \'post_title\' => $fixtureoppteam,
         \'post_name\' => $fixtureoppteam,
     );

     // unhook this function so it doesn\'t loop infinitely
     remove_action(\'save_post\', \'save_custom_post_form\');

     // update the post, which calls save_post again
     wp_update_post( $posttitle );


     // re-hook this function
     add_action(\'save_post\', \'save_custom_post_form\');
}

EDIT

好的,经过一点调试,我发现wp\\u is\\u post\\u revision($post)返回了$post的post ID,这就是为什么post更新代码没有被触发的原因。

在查看原始数据库后,我得出了以下结论。

当我添加新帖子时,数据库中有两个条目,状态分别为“继承”和“发布”。$post似乎使用了“继承”帖子的id,这就是为什么wp_is_post_revision ( $post ) 正在失败。奇怪的是update_post_meta ( ... ) 条目还使用$post 但是元条目最终链接到正确的帖子(“发布”帖子)。

我仍然不知道如何在不删除wp_is_post_revision ( $post ) 检查,我认为这不是一个好主意。

1 个回复
最合适的回答,由SO网友:Ben Wainwright 整理而成

好的,在阅读了“save\\u post”钩子上的codex页面后,我发现他们使用了不同的方式来处理修订。因此,删除if {...} 围绕wp_update_post( $posttitle ) 插入以下代码修复了我的问题:

if ( $post = wp_is_post_revision( $post ) )
        $post = $post; 

结束

相关推荐