How to hide CPT files from media library programmatically

Question

We have created a CPT which allows our customer to upload files from the admin page to a folder outside of the wp-content/uploads folder. Files are uploaded and manipulated using wp_handle_upload and wp_insert_attachment.

How do we prevent these files from showing in the media library? Can we filter by post type?

Thanks in advance for any help!

UPDATE: here’s the code we’ve implemented so far. New uploads are still not showing in the media library.

/*FORCE LIST VIEW IN MEDIA LIBRARY*/
add_action('admin_init', function() {
    $_GET['mode'] = 'list';
}, 100);

/*HIDE GRID BUTTON IN MEDIA LIBRARY*/
add_action('admin_head', function() {
    $css = '<style type="text/css">
        .view-switch .view-grid {
            display: none;
        }
    <style>';
    echo $css;
});

/*REGISTER CUSTOM TAXONOMY*/
function create_hidden_taxonomy() {
    register_taxonomy(
        'hidden_taxonomy',
        'attachment',
        array(
            'label' => __( 'Hidden Taxonomy' ),
            'public' => false,
            'rewrite' => false,
            'hierarchical' => false,
        )
    );
}
add_action( 'init', 'create_hidden_taxonomy' );

/*CHECK IF PARENT POST TYPE IS ASSET. IF NOT ADD 'show_in_media_library' TERM*/
function assets_add_term( $post_id, WP_Post $p, $update ) {

    if ( 'attachment' !== $p->post_type ) {
        error_log("fail1");            
        return;
    }
    if ( wp_is_post_revision( $post_id ) ) {
        error_log("fail2");            
        return;
    }
    if ( $post->post_parent ) {
        $excluded_types = array( 'assets' );
        if ( in_array( get_post_type( $p->post_parent ), $excluded_types ) ) {
        error_log("fail3");                
        return;
        }
    }
    $result = wp_set_object_terms( $post_id, 'show_in_media_library', 'hidden_taxonomy', false );
    if ( !is_array( $result ) || is_wp_error( $result ) ) {
        error_log("fail4");
    }else{
        error_log("it worked!");
    }
}
add_action( 'save_post', 'assets_add_term', 10, 2 );

/*HIDE MEDIA WITH CPT ASSETS FROM MEDIA LIBRARY*/
function assets_load_media() {
   add_action('pre_get_posts','assets_hide_media',10,1);
}
add_action( 'load-upload.php' , 'assets_load_media' );

function assets_hide_media($query){
    global $pagenow;

// there is no need to check for update.php as we are already hooking to it, but anyway
    if( 'upload.php' != $pagenow  || !is_admin())
        return;

    if(is_main_query()){
        $excluded_cpt_ids = get_posts('post_type=assets&posts_per_page=-1&fields=ids');
        $query->set('post_parent__not_in', $excluded_cpt_ids);
        //$query->set('hidden_taxonomy', 'show_in_media_library' );
    }

    return $query;
}

/*HIDE MEDIA WITH CPT ASSETS FROM MEDIA LIBRARY MODAL*/
function assets_hide_media_modal( $query = array() ){
    $query['post_parent__not_in'] = $excluded_cpt_ids;
    return $query;
}
add_action('ajax_query_attachments_args','assets_hide_media_modal',10,1);
0
, , Richard Tinkler 2 years 2020-08-27T11:10:22-05:00 0 Answers 48 views 0

Leave an answer

Browse
Browse