Reputation: 11
I have created a custom post type in WordPress but wondering if its possible to specify an upload directory specific to the custom post type and when doing and inserting of media that the media library only show the media uploaded into the specific directory for the custom post type.
save images in and media library only shows media in:
/wp-content/uplodas/custom_post_type/
UPDATE
I have the following code which will filter the content i'm looking for HOWEVER i only want to run the filter if im on the CPT of client_gallery. My if statment doesnt seem to work?
function wpse48677_get_cpt()
{
if ($_GET['post_type'] == "") {
$posts = $_GET['post'];
$postType = get_post_type($posts);
} else {
$postType = $_GET['post_type'];
}
return $postType;
}
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 = 'client_gallery') ";
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;
}
print wpse48677_get_cpt();
if(wpse48677_get_cpt() == 'client_gallery') {
// THIS IF STATEMENT DOESNT SEEM TO WORK? I KNOW $doJoin == client_gallery
// AS I CAN SEE IT IF I PRINT IT OUT (see above)
// IF I RUN THE ADD FILTER WITHOUT THE IF I GET THE RESULTS I"M LOOKING FOR?????
add_filter('ajax_query_attachments_args', 'my_bind_media_uploader_special_filters');
}
Upvotes: 1
Views: 1662
Reputation: 743
you will make an upload dir for your custom post type if you add this code wherever your custom post type declaration happens. this will also use this folder everytime your upload happens from a post where the post type is yours.
function super_upload_dir( $args ) {
if(!isset($_REQUEST['post_id']) || get_post_type( $id ) != $YOUR_POST_TYPE)
return $args;
$id = ( isset( $_REQUEST['post_id'] ) ? $_REQUEST['post_id'] : '' );
if( $id ) {
$newdir = '/' . $DIRECTORY_NAME;
$args['path'] = str_replace( $args['subdir'], '', $args['path'] );
$args['url'] = str_replace( $args['subdir'], '', $args['url'] );
$args['subdir'] = $newdir;
$args['path'] .= $newdir;
$args['url'] .= $newdir;
}
return $args;
}
add_filter( 'upload_dir', 'super_upload_dir' );
for the librairy filter, ive used plugins in the past that had a way to filter media items based on a shortcode, but i wouldnt know off the bat what to do to get it filtered based on current post type in the backend. sorry
Upvotes: 2