当一篇文章从草稿改为发表时,我想改变作者。我有$_GET[\'auth_id\']
如下所示的后期编辑屏幕中的变量...wp-admin/post.php?post=53&action=edit&auth_id=5
. 我尝试save\\u post hook更改帖子作者,如下所示
function change_pos_auth($post_id){
if ( ! wp_is_post_revision( $post_id ) ){
// unhook this function so it doesn\'t loop infinitely
remove_action(\'save_post\',\'change_pos_auth\');
if ( isset($_GET[\'auth_id\']) ) {
$args = array(\'ID\'=>$post_id,\'post_author\'=>$_GET[\'auth_id\']);
// update the post, which calls save_post again
wp_update_post( $args );
}
// re-hook this function
add_action(\'save_post\',\'change_pos_auth\');
}
}
这行不通,可能是这可能没有
$_GET
变量。我尝试让当前用户将其作为作者
function change_pos_auth($post_id){
if ( ! wp_is_post_revision( $post_id ) ){
// unhook this function so it doesn\'t loop infinitely
remove_action(\'save_post\',\'change_pos_auth\');
if ( $last_user = get_post_meta( $post_id, \'_edit_last\', true) ) {
$args = array(\'ID\'=>$post_id,\'post_author\'=>$last_user);
// update the post, which calls save_post again
wp_update_post( $args );
}
// re-hook this function
add_action(\'save_post\',\'change_pos_auth\');
}
}
以上也不起作用。主要思想是,这是一个问题&;答案类型。首先在管理中回答问题的作者将成为该帖子的作者。默认情况下,管理员将询问问题。所以,当它发布时,我试图在save上更改作者。如果帖子已经发布,我不允许其他作者进入编辑屏幕。我将重定向到另一个页面,并说已经有人回答了。
有什么帮助吗?
SO网友:Steven Jones
从您发布的代码来看,似乎在任何时候都不会将函数挂接到save_post
超出您的职能范围。
function change_pos_auth($post_id){
if ( ! wp_is_post_revision( $post_id ) ){
// unhook this function so it doesn\'t loop infinitely
remove_action(\'save_post\',\'change_pos_auth\');
if ( isset($_GET[\'auth_id\']) ) {
$args = array(\'ID\'=>$post_id,\'post_author\'=>$_GET[\'auth_id\']);
// update the post, which calls save_post again
wp_update_post( $args );
}
// re-hook this function
add_action(\'save_post\',\'change_pos_auth\');
}
}
add_action(\'save_post\', \'change_pos_auth\');
剩下的代码看起来还可以
$_GET[\'auth_id\'] 已填充。
注意:在将该值放入数据库之前,不要忘记对其进行清理