我可以向媒体隐藏我从前端上传的附件吗?

时间:2017-06-28 作者:Toni Michel Caubet

我有一些部分,用户可以上传简历和个人资料图像,

我正在使用本机附件函数,

$attachment_cv_id = media_handle_upload( \'candidate_cv\', 0 );
if ( ! is_wp_error( $attachment_cv_id ) ) {
    // We link this file to the post 
    // add_post_meta( $result , \'wpcf-cv-file\', wp_get_attachment_url($attachment_cv_id));
} else {
    $submit_candidate_errors[] = lang("Hubo error subiendo la CV");
}
是否有任何方法可以从wp admin的媒体部分隐藏此文件?(可能正在更改其文件夹)

谢谢

-编辑-

我正按照Dave的建议尝试,添加了一个meta:“rrhh文件”

$attachment_id = media_handle_upload( \'apply_file\', 0 );

if ( ! is_wp_error( $attachment_id ) ) {
    // The image was uploaded successfully!
    add_post_meta($attachment_id, \'rrhh-file\', true);
}
即使它具有以下属性:

var_dump(get_post_meta(1200)); // last id I uploaded
die();
打印输出:

array(4) {
  ["_wp_attached_file"]=>
  array(1) {
    [0]=>
    string(20) "2017/06/image001.jpg"
  }
  ["_wp_attachment_metadata"]=>
  array(1) {
    [0]=>
    string(666) "a:5:{s:5:"width";i:523;s:6:"height";i:122;s:4:"file";s:20:"2017/06/image001.jpg";s:5:"sizes";a:2:{s:9:"thumbnail";a:4:{s:4:"file";s:20:"image001-150x122.jpg";s:5:"width";i:150;s:6:"height";i:122;s:9:"mime-type";s:10:"image/jpeg";}s:6:"medium";a:4:{s:4:"file";s:19:"image001-300x70.jpg";s:5:"width";i:300;s:6:"height";i:70;s:9:"mime-type";s:10:"image/jpeg";}}s:10:"image_meta";a:12:{s:8:"aperture";s:1:"0";s:6:"credit";s:0:"";s:6:"camera";s:0:"";s:7:"caption";s:0:"";s:17:"created_timestamp";s:1:"0";s:9:"copyright";s:0:"";s:12:"focal_length";s:1:"0";s:3:"iso";s:1:"0";s:13:"shutter_speed";s:1:"0";s:5:"title";s:0:"";s:11:"orientation";s:1:"1";s:8:"keywords";a:0:{}}}"
  }
  ["rrhh-file"]=> // <-- here
  array(1) {
    [0]=>
    string(1) "1"
  }
  ["_edit_lock"]=>
  array(1) {
    [0]=>
    string(12) "1498731595:1"
  }
}
执行此操作后,此附件仍会显示在媒体库中

add_action( \'pre_get_posts\', \'wpse_hide_cv_media_list_view\' );
function wpse_hide_cv_media_list_view( $query ) {
    // Bail if this is not the admin area.
    if ( ! is_admin() ) {
        return;
    }

    // Bail if this is not the main query.
    if ( ! $query->is_main_query() ) {
        return;
    }

    // Only proceed if this the attachment upload screen.
    $screen = get_current_screen();
    if ( ! $screen || \'upload\' !== $screen->id || \'attachment\' !== $screen->post_type ) {
        return;
    }

    // Modify the query.
    $query->set( \'meta_query\', [
        [
            \'key\'     => \'rrhh-file\',
            \'compare\' => \'NOT EXISTS\',
        ]
    ]);

    return;
}
知道我错过了什么吗?

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

WordPress有两种不同的媒体显示方式。有媒体覆盖/模式视图,还有列表视图,导航到管理时会触发该视图>;媒体(&T);然后单击过滤器选择框旁边的列表视图图标。要过滤两个位置的附件,我们需要使用两个单独的挂钩。

Filtering attachments in the Media Library (modal view)

这个ajax_query_attachments_args 过滤器可用于调整用于在媒体覆盖屏幕上获取附件的查询。(改编自this answer 来自birgire。)

/**
 * Hide attachment files from the Media Library\'s overlay (modal) view
 * if they have a certain meta key set.
 * 
 * @param array $args An array of query variables.
 */
add_filter( \'ajax_query_attachments_args\', \'wpse_hide_cv_media_overlay_view\' );
function wpse_hide_cv_media_overlay_view( $args ) {
    // Bail if this is not the admin area.
    if ( ! is_admin() ) {
        return $args;
    }

    // Modify the query.
    $args[\'meta_query\'] = [
        [
            \'key\'     => \'rrhh-file\',
            \'compare\' => \'NOT EXISTS\',
        ]
    ];
   
    return $args;
}

Filtering attachments in the Media Library (list view)

我们需要一种额外的、单独的方法来修改列表视图(旧的列表模式,非模式视图)中媒体库屏幕上的附件查询。这个pre_get_posts 胡克会成功的:

/**
 * Hide attachment files from the Media Library\'s list view
 * if they have a certain meta key set.
 * 
 * @param WP_Query $query The WP_Query instance (passed by reference).
 */
add_action( \'pre_get_posts\', \'wpse_hide_cv_media_list_view\' );
function wpse_hide_cv_media_list_view( $query ) {
    // Bail if this is not the admin area.
    if ( ! is_admin() ) {
        return;
    }

    // Bail if this is not the main query.
    if ( ! $query->is_main_query() ) {
        return;
    }

    // Only proceed if this the attachment upload screen.
    $screen = get_current_screen();
    if ( ! $screen || \'upload\' !== $screen->id || \'attachment\' !== $screen->post_type ) {
        return;
    }
    
    // Modify the query.
    $query->set( \'meta_query\', [
        [
            \'key\'     => \'rrhh-file\',
            \'compare\' => \'NOT EXISTS\',
        ]
    ]   );

    return;
}
在这两种情况下,我们都设置了一个元查询,以排除任何具有元键的附件rrhh-file 设置您可以更改元查询或条件以满足您的需要。

请注意,元查询速度很慢。另一种方法是,在上载/创建这些特殊媒体项目时,将其分配给自定义分类法,然后更改查询以使用税务查询排除这些项目。

Edit: Debugging info

我使用以下功能可以更轻松地编辑自定义附件字段:

/**
 * Add custom field to media
 */
add_filter( \'attachment_fields_to_edit\', \'wpse_cv_attachment_fields\', 10, 2 );
function wpse_cv_attachment_fields( $fields, $post ) {
    $meta = get_post_meta( $post->ID, \'rrhh-file\', true );

    $fields[\'rrhh-file\'] = array(
            \'label\' =>  __( \'RRHH File\', \'text-domain\' ),
            \'input\' => \'text\',
            \'value\' => $meta,
            \'show_in_edit\' => true,
    );

    return $fields;
}

/**
 * Update custom field within media overlay (via ajax)
 */
add_action( \'wp_ajax_save-attachment-compat\', \'wpse_cv_media_fields\', 0, 1 );
function wpse_cv_media_fields() {
    $post_id = $_POST[\'id\'];
    $value = isset( $_POST[\'attachments\'][ $post_id ][\'rrhh-file\'] ) ? $_POST[\'attachments\'][ $post_id ][\'rrhh-file\'] : false;
    
    if ( \'1\' === $value ) {
        update_post_meta( $post_id, \'rrhh-file\', $value );
    } else {
        delete_post_meta( $post_id, \'rrhh-file\' );
    }

    clean_post_cache( $post_id );
}

/**
 * Update media custom field from edit media page (non ajax).
 */
add_action( \'edit_attachment\', \'wpse_cv_update_attachment_meta\', 1 );
function wpse_cv_update_attachment_meta( $post_id ) {
    $value = isset( $_POST[\'attachments\'][ $post_id ][\'rrhh-file\'] ) ? $_POST[\'attachments\'][ $post_id ][\'rrhh-file\'] : false;
    //exit ( var_dump( $value ) );
    if ( \'1\' === $value ) {
        update_post_meta( $post_id, \'rrhh-file\', $value );
    } else {
        delete_post_meta( $post_id, \'rrhh-file\' );
    }

    return;
}
我使用以下SQL检查rrhh-file 字段已设置:

SELECT * FROM wp_postmeta w where meta_key = \'rrhh-file\';
在列表模式下查看媒体库时,应受到pre_get_posts$query->is_main_query() 正在返回true$screen, 这个WP_Screen 对象,如下所示:

WP_Screen Object
(
    [action] => 
    [base] => upload
    [columns:WP_Screen:private] => 0
    [id] => upload
    [in_admin:protected] => site
    [is_network] => 
    [is_user] => 
    [parent_base] => 
    [parent_file] => 
    [post_type] => attachment
    [taxonomy] => 
    [_help_tabs:WP_Screen:private] => Array
        (
        )

    [_help_sidebar:WP_Screen:private] => 
    [_screen_reader_content:WP_Screen:private] => Array
        (
        )

    [_options:WP_Screen:private] => Array
        (
        )

    [_show_screen_options:WP_Screen:private] => 
    [_screen_settings:WP_Screen:private] => 
)
以及$query, WP\\U查询对象如下所示:

WP_Query Object
(
    [query] => Array
        (
            [m] => 0
            [cat] => 0
            [post_type] => attachment
            [posts_per_page] => 20
            [post_status] => inherit,private
        )

    [query_vars] => Array
        (
            [m] => 0
            [cat] => 0
            [post_type] => attachment
            [posts_per_page] => 20
            [post_status] => inherit,private
            [error] => 
            [p] => 0
            [post_parent] => 
            [subpost] => 
            [subpost_id] => 
            [attachment] => 
            [attachment_id] => 0
            [name] => 
            [static] => 
            [pagename] => 
            [page_id] => 0
            [second] => 
            [minute] => 
            [hour] => 
            [day] => 0
            [monthnum] => 0
            [year] => 0
            [w] => 0
            [category_name] => 
            [tag] => 
            [tag_id] => 
            [author] => 
            [author_name] => 
            [feed] => 
            [tb] => 
            [paged] => 0
            [meta_key] => 
            [meta_value] => 
            [preview] => 
            [s] => 
            [sentence] => 
            [title] => 
            [fields] => 
            [menu_order] => 
            [embed] => 
            [category__in] => Array
                (
                )

            [category__not_in] => Array
                (
                )

            [category__and] => Array
                (
                )

            [post__in] => Array
                (
                )

            [post__not_in] => Array
                (
                )

            [post_name__in] => Array
                (
                )

            [tag__in] => Array
                (
                )

            [tag__not_in] => Array
                (
                )

            [tag__and] => Array
                (
                )

            [tag_slug__in] => Array
                (
                )

            [tag_slug__and] => Array
                (
                )

            [post_parent__in] => Array
                (
                )

            [post_parent__not_in] => Array
                (
                )

            [author__in] => Array
                (
                )

            [author__not_in] => Array
                (
                )

        )

    [tax_query] => WP_Tax_Query Object
        (
            [queries] => Array
                (
                )

            [relation] => AND
            [table_aliases:protected] => Array
                (
                )

            [queried_terms] => Array
                (
                )

            [primary_table] => 
            [primary_id_column] => 
        )

    [meta_query] => 
    [date_query] => 
    [post_count] => 0
    [current_post] => -1
    [in_the_loop] => 
    [comment_count] => 0
    [current_comment] => -1
    [found_posts] => 0
    [max_num_pages] => 0
    [max_num_comment_pages] => 0
    [is_single] => 
    [is_preview] => 
    [is_page] => 
    [is_archive] => 
    [is_date] => 
    [is_year] => 
    [is_month] => 
    [is_day] => 
    [is_time] => 
    [is_author] => 
    [is_category] => 
    [is_tag] => 
    [is_tax] => 
    [is_search] => 
    [is_feed] => 
    [is_comment_feed] => 
    [is_trackback] => 
    [is_home] => 
    [is_404] => 
    [is_embed] => 
    [is_paged] => 
    [is_admin] => 1
    [is_attachment] => 
    [is_singular] => 
    [is_robots] => 
    [is_posts_page] => 
    [is_post_type_archive] => 
    [query_vars_hash:WP_Query:private] => b01f0d78a3a985d46374d4384bba30b7
    [query_vars_changed:WP_Query:private] => 
    [thumbnails_cached] => 
    [stopwords:WP_Query:private] => 
    [compat_fields:WP_Query:private] => Array
        (
            [0] => query_vars_hash
            [1] => query_vars_changed
        )

    [compat_methods:WP_Query:private] => Array
        (
            [0] => init_query_flags
            [1] => parse_tax_query
        )

)

结束

相关推荐

how to edit attachments?

在将例如文件附加到帖子时,如何在事后编辑/删除它们?在帖子编辑器中找不到任何内容。谢谢