Media library – Limit images to custom post type

The question:

Is there some wordpress magic/plugin that will make the media library only show images that were uploaded to a specific custom post type?
I have a custom post type called “artists”, I want, when admin click to upload/attach an image, that the media library popup only show images that have been uploaded to the artists custom type, and not the entire site.

I use the ACF plugin for handling custom fields, and custom post types ui.
Is this possible?

The Solutions:

Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.

Method 1

I’m not 100% sure if I get your problem right, but… Maybe this will help you…

Media uploader gets attachments with simple WP_Query, so you can use many filters to modify it’s contents.

The only problem is that you can’t query posts with specific CPT as parent using WP_Query arguments… So, we will have to use posts_where and posts_join filters.

To be sure, that we’ll change only media uploader’s query, we’ll use ajax_query_attachments_args.

And this is how it looks, when combined:

function my_posts_where($where) {
    global $wpdb;

    $post_id = false;
    if ( isset($_POST['post_id']) ) {
        $post_id = $_POST['post_id'];

        $post = get_post($post_id);
        if ( $post ) {
            $where .= $wpdb->prepare(" AND my_post_parent.post_type = %s ", $post->post_type);
        }
    }

    return $where;
}

function my_posts_join($join) {
    global $wpdb;

    $join .= " LEFT JOIN {$wpdb->posts} as my_post_parent ON ({$wpdb->posts}.post_parent = my_post_parent.ID) ";

    return $join;
}


function my_bind_media_uploader_special_filters($query) {
    add_filter('posts_where', 'my_posts_where');
    add_filter('posts_join', 'my_posts_join');

    return $query;
}
add_filter('ajax_query_attachments_args', 'my_bind_media_uploader_special_filters');

When you open media uploader dialog while editing post (post/page/CPT), you’ll see only images attached to this specific post type.

If you’d like it to work only for one specific post type (let’s say pages), you’ll have to change condition in my_posts_where function like so:

function my_posts_where($where) {
    global $wpdb;

    $post_id = false;
    if ( isset($_POST['post_id']) ) {
        $post_id = $_POST['post_id'];

        $post = get_post($post_id);
        if ( $post && 'page' == $post->post_type ) {  // you can change 'page' to any other post type
            $where .= $wpdb->prepare(" AND my_post_parent.post_type = %s ", $post->post_type);
        }
    }

    return $where;
}

Method 2

Display only the images of the property when editing the featured image

function my_bind_media_uploader_special_filters($query) 
{

    add_filter('posts_where', 'my_posts_where');
    return $query;
}

add_filter('ajax_query_attachments_args','my_bind_media_uploader_special_filters');

function my_posts_where ($where) 
{

    global $wpdb;
    $post_id = false;
    if ( isset($_POST['post_id']) ) {
        $post_id = $_POST['post_id'];
        $post = get_post($post_id);
        if ( $post && 'property' == $post->post_type) {
            $where .= $wpdb->prepare(" AND id in (select distinct meta_value from 
            wpdb_postmeta where meta_key='fave_property_images' and post_id = $post_id)", 
            $post->post_type);
        }
    }
    return $where;
}


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Comment