【问题标题】:WordPress insert_attachment set post_authorWordPress insert_attachment 设置 post_author
【发布时间】:2013-09-21 15:43:42
【问题描述】:

我想在 WordPress 中从前端发布附件,这是我的有效代码:

表单流程:

global $post;

if ( $_FILES ) {
    $files = $_FILES['upload_attachment'];
    foreach ($files['name'] as $key => $value) {
        if ($files['name'][$key]) {
            $file = array(
                'name' => $files['name'][$key],
                'type' => $files['type'][$key],
                'tmp_name' => $files['tmp_name'][$key],
                'error' => $files['error'][$key],
                'size' => $files['size'][$key]
            );

            $_FILES = array("upload_attachment" => $file);

            foreach ($_FILES as $file => $array) {
                $newupload = insert_attachment($file,0);
            }
        }
    }
}

处理上传的功能:

function insert_attachment($file_handler,$post_id,$setthumb='false') {
    // check to make sure its a successful upload
    if ($_FILES[$file_handler]['error'] !== UPLOAD_ERR_OK) __return_false();

    require_once(ABSPATH . "wp-admin" . '/includes/image.php');
    require_once(ABSPATH . "wp-admin" . '/includes/file.php');
    require_once(ABSPATH . "wp-admin" . '/includes/media.php');

    $attach_id = media_handle_upload( $file_handler, $post_id );

    if ($setthumb) update_post_meta($post_id,'_thumbnail_id',$attach_id);
    return $attach_id;
}

我想设置附件的post_author,因为当前默认为当前登录用户。

我尝试在函数中添加以下内容,但似乎不起作用:

$my_post = array(
    'ID'          => $attach_id,
    'post_author' => 2
);
wp_update_post( $my_post );

【问题讨论】:

    标签: wordpress upload attachment


    【解决方案1】:
    if ($setthumb) {
        update_post_meta($post_id, '_thumbnail_id', $attach_id);
    }
    

    好吧,根据 wp_update_post(codex) 你必须通过POST_ID 我不确定你的$attach_id 是不是帖子ID,根据update_post_meta($post_id, '_thumbnail_id', $attach_id); 通过$post_id

    $my_post = array(
      'ID' => $post_id,
      'post_author' => 2
    );
    wp_update_post( $my_post );
    

    【讨论】:

    • 最初我确实传递了 $post_id,但这复制了我发布的页面,并且无论如何都没有更改 $attachment 作者
    【解决方案2】:

    不太清楚发生了什么,我重试了这个,似乎成功了!只是想更新以防其他人有同样的问题!

       $my_post = array(
          'ID'           => $attach_id,
          'post_author' => 2
        );
        wp_update_post( $my_post );
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-12-10
      • 1970-01-01
      • 2015-12-05
      • 2016-03-22
      • 2018-07-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多