How to change upload directory for custom post type?

WordPress: Is there a way to change location of uploads folder for a specific custom post type only?

Adapt this code to your CPT, and add it in your plugin or function file:

<?php
/*
* Change upload directory for CPT 'places'
*/
function wpster_places_upload_dir($args) {
	$id = $_REQUEST['post_id'];
	if("places"==get_post_type($id)) {
	 $newdir = '/'.get_post_type($id);
	 $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;
}
?>

Submit a Comment

Your email address will not be published. Required fields are marked *