【问题标题】:Wordpress: Can I edit table fields while media uploading?Wordpress:我可以在媒体上传时编辑表格字段吗?
【发布时间】:2015-07-09 04:00:42
【问题描述】:

当我挂钩媒体上传(我使用add_attachment挂钩)时,有没有办法编辑添加到数据库中的值? 基本上,我想做的是:

  • 媒体上传挂钩

  • 根据 $attachment_id 编辑/更新 wp_posts 表中的字段值(post_status)

  • 返回

函数如下:

add_filter('add_attachment', 'handle_upload' );

function handle_upload($attachment_id){

//get attachment ID from database: how? I believe that at this point the database is not populated with the new post (media)

// update the post_status field from wp_posts table with some value

//return
}

这可能吗?

提前致谢!

【问题讨论】:

  • 您想收集什么信息?或者你想更新什么?
  • 嗨,我正在开发一个插件,它不会在上传后立即显示图像,而是在审核后显示。所以,我需要做的是: - 挂钩媒体上传事件(例如:add_attachment,如果这不是正确的,请指向另一个) - 将帖子的状态更改为“auto_draft”
  • 啊,所以当一个项目被上传并添加到媒体库时,您需要设置一个审核标志。你来对地方了。让我挖掘一下,看看能否为您找到更多信息。
  • 太棒了!谢谢!我使用了“wp_update_post”,但我认为我尝试过早地编辑标志(在它登陆数据库之前)
  • 我设法做到了。我会发布结果!感谢您的支持!

标签: php wordpress upload media attachment


【解决方案1】:

我找到了解决方案。 解决方法是使用wp_update_post:

function update_db_field($attachment_ID){

    $attachment_details = get_post( $attachment_ID );

    wp_update_post( array(
            'ID' => $attachment_details->ID ,
            'post_type' => "attachment"
        )
    );

}

add_action("add_attachment", 'update_db_field');

我希望它可以帮助别人!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-01-21
    • 1970-01-01
    • 1970-01-01
    • 2012-07-25
    • 2012-12-17
    • 2017-06-28
    • 2022-11-04
    • 1970-01-01
    相关资源
    最近更新 更多